ProcessStarter.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. namespace Azylee.Core.ProcessUtils
  8. {
  9. public static class ProcessStarter
  10. {
  11. /// <summary>
  12. /// 创建进程
  13. /// </summary>
  14. /// <returns></returns>
  15. public static Process NewProcess(string exe, string args = "")
  16. {
  17. ProcessStartInfo startInfo = new ProcessStartInfo();
  18. startInfo.FileName = exe;
  19. startInfo.Arguments = args;
  20. startInfo.RedirectStandardInput = true;
  21. startInfo.RedirectStandardOutput = true;
  22. startInfo.RedirectStandardError = true;
  23. startInfo.UseShellExecute = false;
  24. startInfo.CreateNoWindow = true;
  25. startInfo.Verb = "RunAs";
  26. Process process = new Process();
  27. process.StartInfo = startInfo;
  28. return process;
  29. }
  30. /// <summary>
  31. /// 开始运行
  32. /// </summary>
  33. /// <param name="process"></param>
  34. /// <param name="output"></param>
  35. public static void Execute(Process process, Action<string> output)
  36. {
  37. StreamReader reader = null;
  38. try
  39. {
  40. process.Start();
  41. process.StandardInput.AutoFlush = true;
  42. reader = process.StandardOutput;
  43. do
  44. {
  45. string line = reader.ReadLine();
  46. output?.Invoke(line);
  47. } while (!reader.EndOfStream);
  48. process.WaitForExit();
  49. process.Close();
  50. }
  51. catch { }
  52. }
  53. }
  54. }