Shell.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace ShareWifi.Utils
  8. {
  9. class Shell
  10. {
  11. /// <summary>
  12. /// 执行CMD语句
  13. /// </summary>
  14. /// <param name="cmd">要执行的CMD命令</param>
  15. public static string RunCmd(string cmd)
  16. {
  17. Process proc = new Process();
  18. proc.StartInfo.CreateNoWindow = true;
  19. proc.StartInfo.FileName = "cmd.exe";
  20. proc.StartInfo.UseShellExecute = false;
  21. proc.StartInfo.RedirectStandardError = true;
  22. proc.StartInfo.RedirectStandardInput = true;
  23. proc.StartInfo.RedirectStandardOutput = true;
  24. proc.Start();
  25. proc.StandardInput.WriteLine(cmd);
  26. proc.StandardInput.WriteLine("exit");
  27. proc.StandardInput.AutoFlush = true;
  28. string outStr = proc.StandardOutput.ReadToEnd();
  29. proc.WaitForExit();
  30. proc.Close();
  31. return outStr;
  32. }
  33. /// <summary>
  34. /// 打开软件并执行命令
  35. /// </summary>
  36. /// <param name="programName">软件路径加名称(.exe文件)</param>
  37. /// <param name="cmd">要执行的命令</param>
  38. public static void RunProgram(string programName, string cmd)
  39. {
  40. Process proc = new Process();
  41. proc.StartInfo.CreateNoWindow = true;
  42. proc.StartInfo.FileName = programName;
  43. proc.StartInfo.UseShellExecute = false;
  44. proc.StartInfo.RedirectStandardError = true;
  45. proc.StartInfo.RedirectStandardInput = true;
  46. proc.StartInfo.RedirectStandardOutput = true;
  47. proc.Start();
  48. if (cmd.Length != 0)
  49. {
  50. proc.StandardInput.WriteLine(cmd);
  51. }
  52. proc.Close();
  53. }
  54. /// <summary>
  55. /// 打开软件
  56. /// </summary>
  57. /// <param name="programName">软件路径加名称(.exe文件)</param>
  58. public static void RunProgram(string programName)
  59. {
  60. RunProgram(programName, "");
  61. }
  62. }
  63. }