ShellCmd.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace EasyTemplate.Tool;
  8. public class ShellCmd
  9. {
  10. public static string Bash(string command)
  11. {
  12. var escapedArgs = command.Replace("\"", "\\\"");
  13. var process = new Process()
  14. {
  15. StartInfo = new ProcessStartInfo
  16. {
  17. FileName = "/bin/bash",
  18. Arguments = $"-c \"{escapedArgs}\"",
  19. RedirectStandardOutput = true,
  20. UseShellExecute = false,
  21. CreateNoWindow = true,
  22. }
  23. };
  24. process.Start();
  25. string result = process.StandardOutput.ReadToEnd();
  26. process.WaitForExit();
  27. process.Dispose();
  28. return result;
  29. }
  30. public static string Cmd(string fileName, string args)
  31. {
  32. string output = string.Empty;
  33. var info = new ProcessStartInfo();
  34. info.FileName = fileName;
  35. info.Arguments = args;
  36. info.RedirectStandardOutput = true;
  37. using (var process = Process.Start(info))
  38. {
  39. output = process.StandardOutput.ReadToEnd();
  40. }
  41. return output;
  42. }
  43. }