CMDProcessTool.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //************************************************************************
  2. // author: yuzhengyang
  3. // date: 2018.4.27 - 2018.5.30
  4. // desc: CMD 工具
  5. // Copyright (c) yuzhengyang. All rights reserved.
  6. //************************************************************************
  7. using Azylee.Core.DataUtils.StringUtils;
  8. using Azylee.Core.ThreadUtils.SleepUtils;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.IO;
  13. using System.Threading.Tasks;
  14. namespace Azylee.Core.WindowsUtils.CMDUtils
  15. {
  16. public class CMDProcessTool
  17. {
  18. /// <summary>
  19. /// 创建cmd的进程
  20. /// </summary>
  21. /// <returns></returns>
  22. public static Process GetProcess(string verb = "RunAs")
  23. {
  24. ProcessStartInfo startInfo = new ProcessStartInfo();
  25. startInfo.FileName = "cmd.exe";
  26. startInfo.Arguments = @"/c C:\Windows\System32\cmd.exe";
  27. startInfo.RedirectStandardInput = true;
  28. startInfo.RedirectStandardOutput = true;
  29. startInfo.RedirectStandardError = true;
  30. startInfo.UseShellExecute = false;
  31. startInfo.CreateNoWindow = true;
  32. startInfo.Verb = verb;
  33. Process process = new Process();
  34. process.StartInfo = startInfo;
  35. return process;
  36. }
  37. /// <summary>
  38. /// 开始运行CMD命令
  39. /// </summary>
  40. /// <param name="cmd"></param>
  41. /// <param name="output">输出动作</param>
  42. public static void Execute(string cmd, Action<string> output)
  43. {
  44. StreamReader reader = null;
  45. Process process = null;
  46. try
  47. {
  48. process = GetProcess();
  49. process.Start();
  50. process.StandardInput.AutoFlush = true;
  51. process.StandardInput.WriteLine(cmd);
  52. process.StandardInput.WriteLine("exit");
  53. reader = process.StandardOutput;
  54. do
  55. {
  56. string line = reader.ReadLine();
  57. output?.Invoke(line);
  58. } while (!reader.EndOfStream);
  59. process.WaitForExit();
  60. process.Close();
  61. }
  62. catch { }
  63. }
  64. /// <summary>
  65. /// 运行CMD并读取结果(建议执行返回数据较小的命令)
  66. /// </summary>
  67. /// <param name="cmd"></param>
  68. /// <returns></returns>
  69. public static List<string> Execute(string cmd)
  70. {
  71. List<string> result = null;
  72. StreamReader reader = null;
  73. Process process = null;
  74. try
  75. {
  76. process = GetProcess();
  77. process.Start();
  78. process.StandardInput.WriteLine(cmd);
  79. process.StandardInput.WriteLine("exit");
  80. reader = process.StandardOutput;
  81. result = new List<string>();
  82. do
  83. {
  84. string line = reader.ReadLine();
  85. if (Str.Ok(line)) result.Add(line.Trim());
  86. } while (!reader.EndOfStream);
  87. process.WaitForExit();
  88. }
  89. catch { }
  90. finally
  91. {
  92. reader?.Close();
  93. process?.Close();
  94. process?.Dispose();
  95. }
  96. return result;
  97. }
  98. }
  99. }