ShellUtil.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. namespace Admin.NET.Core;
  2. /// <summary>
  3. /// 系统Shell命令
  4. /// </summary>
  5. public class ShellUtil
  6. {
  7. /// <summary>
  8. /// Bash命令
  9. /// </summary>
  10. /// <param name="command"></param>
  11. /// <returns></returns>
  12. public static string Bash(string command)
  13. {
  14. var escapedArgs = command.Replace("\"", "\\\"");
  15. var process = new Process()
  16. {
  17. StartInfo = new ProcessStartInfo
  18. {
  19. FileName = "/bin/bash",
  20. Arguments = $"-c \"{escapedArgs}\"",
  21. RedirectStandardOutput = true,
  22. UseShellExecute = false,
  23. CreateNoWindow = true,
  24. }
  25. };
  26. process.Start();
  27. string result = process.StandardOutput.ReadToEnd();
  28. process.WaitForExit();
  29. process.Dispose();
  30. return result;
  31. }
  32. /// <summary>
  33. /// cmd命令
  34. /// </summary>
  35. /// <param name="fileName"></param>
  36. /// <param name="args"></param>
  37. /// <returns></returns>
  38. public static string Cmd(string fileName, string args)
  39. {
  40. string output = string.Empty;
  41. var info = new ProcessStartInfo
  42. {
  43. FileName = fileName,
  44. Arguments = args,
  45. RedirectStandardOutput = true
  46. };
  47. using (var process = Process.Start(info))
  48. {
  49. output = process.StandardOutput.ReadToEnd();
  50. }
  51. return output;
  52. }
  53. }