CMDProcessTool.cs 4.5 KB

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