CMDProcessTool.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.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. public class CMDProcessTool
  19. {
  20. /// <summary>
  21. /// 创建cmd的进程
  22. /// </summary>
  23. /// <returns></returns>
  24. public static Process GetProcess(string verb = "RunAs", WindowsAccountModel account = null)
  25. {
  26. ProcessStartInfo startInfo = new ProcessStartInfo();
  27. if (account != null && account.Check())
  28. {
  29. if (Str.Ok(account.Domain)) startInfo.Domain = account.Domain;
  30. if (Str.Ok(account.UserName)) startInfo.UserName = account.UserName;
  31. if (Str.Ok(account.Password)) startInfo.Password = ProcessStarter.ConvertToSecureString(account.Password);
  32. }
  33. startInfo.FileName = "cmd.exe";
  34. startInfo.Arguments = @"/c C:\Windows\System32\cmd.exe";
  35. startInfo.RedirectStandardInput = true;
  36. startInfo.RedirectStandardOutput = true;
  37. startInfo.RedirectStandardError = true;
  38. startInfo.UseShellExecute = false;
  39. startInfo.CreateNoWindow = true;
  40. startInfo.Verb = verb;
  41. Process process = new Process();
  42. process.StartInfo = startInfo;
  43. return process;
  44. }
  45. /// <summary>
  46. /// 开始运行CMD命令
  47. /// </summary>
  48. /// <param name="cmd"></param>
  49. /// <param name="output">输出动作</param>
  50. public static void Execute(string cmd, Action<string> output, WindowsAccountModel account = null)
  51. {
  52. StreamReader reader = null;
  53. Process process = null;
  54. try
  55. {
  56. process = GetProcess(account: account);
  57. process.Start();
  58. process.StandardInput.AutoFlush = true;
  59. process.StandardInput.WriteLine(cmd);
  60. process.StandardInput.WriteLine("exit");
  61. reader = process.StandardOutput;
  62. do
  63. {
  64. string line = reader.ReadLine();
  65. output?.Invoke(line);
  66. } while (!reader.EndOfStream);
  67. process.WaitForExit();
  68. process.Close();
  69. }
  70. catch { }
  71. }
  72. /// <summary>
  73. /// 运行CMD并读取结果(建议执行返回数据较小的命令)
  74. /// </summary>
  75. /// <param name="cmd"></param>
  76. /// <returns></returns>
  77. public static List<string> Execute(string cmd, WindowsAccountModel account = null)
  78. {
  79. List<string> result = null;
  80. StreamReader reader = null;
  81. Process process = null;
  82. try
  83. {
  84. process = GetProcess(account: account);
  85. process.Start();
  86. process.StandardInput.WriteLine(cmd);
  87. process.StandardInput.WriteLine("exit");
  88. reader = process.StandardOutput;
  89. result = new List<string>();
  90. do
  91. {
  92. string line = reader.ReadLine();
  93. if (Str.Ok(line)) result.Add(line.Trim());
  94. } while (!reader.EndOfStream);
  95. process.WaitForExit();
  96. }
  97. catch { }
  98. finally
  99. {
  100. reader?.Close();
  101. process?.Close();
  102. process?.Dispose();
  103. }
  104. return result;
  105. }
  106. }
  107. }