ProcessHelper.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4. using System.Threading.Tasks;
  5. using Debug = UnityEngine.Debug;
  6. using Path = System.IO.Path;
  7. namespace ET
  8. {
  9. public static class ProcessHelper
  10. {
  11. public static System.Diagnostics.Process Run(string exe, string arguments, string workingDirectory = ".", bool waitExit = false)
  12. {
  13. //Log.Debug($"Process Run exe:{exe} ,arguments:{arguments} ,workingDirectory:{workingDirectory}");
  14. try
  15. {
  16. bool redirectStandardOutput = false;
  17. bool redirectStandardError = false;
  18. bool useShellExecute = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
  19. if (waitExit)
  20. {
  21. redirectStandardOutput = true;
  22. redirectStandardError = true;
  23. useShellExecute = false;
  24. }
  25. ProcessStartInfo info = new ProcessStartInfo
  26. {
  27. FileName = exe,
  28. Arguments = arguments,
  29. CreateNoWindow = true,
  30. UseShellExecute = useShellExecute,
  31. WorkingDirectory = workingDirectory,
  32. RedirectStandardOutput = redirectStandardOutput,
  33. RedirectStandardError = redirectStandardError,
  34. };
  35. System.Diagnostics.Process process = System.Diagnostics.Process.Start(info);
  36. if (waitExit)
  37. {
  38. process.WaitForExit();
  39. }
  40. return process;
  41. }
  42. catch (Exception e)
  43. {
  44. throw new Exception($"dir: {Path.GetFullPath(workingDirectory)}, command: {exe} {arguments}", e);
  45. }
  46. }
  47. }
  48. }