LibCMD.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 IMCS.Lib
  8. {
  9. public class LibCMD
  10. {
  11. private static LibCMD cMDLib = new LibCMD();
  12. public static LibCMD Instance { get { return cMDLib; } }
  13. public string WriteCmd(string value)
  14. {
  15. Process p = new Process();
  16. //设置要启动的应用程序
  17. p.StartInfo.FileName = "cmd.exe";
  18. //是否使用操作系统shell启动
  19. p.StartInfo.UseShellExecute = false;
  20. // 接受来自调用程序的输入信息
  21. p.StartInfo.RedirectStandardInput = true;
  22. //输出信息
  23. p.StartInfo.RedirectStandardOutput = true;
  24. // 输出错误
  25. p.StartInfo.RedirectStandardError = true;
  26. //不显示程序窗口
  27. p.StartInfo.CreateNoWindow = true;
  28. //启动程序
  29. p.Start();
  30. //向cmd窗口发送输入信息
  31. p.StandardInput.WriteLine(value + "&exit");
  32. p.StandardInput.AutoFlush = true;
  33. //获取输出信息
  34. string strOuput = p.StandardOutput.ReadToEnd();
  35. //等待程序执行完退出进程
  36. p.WaitForExit();
  37. p.Close();
  38. return strOuput;
  39. }
  40. }
  41. }