Packaging: Cmd command calls and frequently used commands

Original: Packaging: Cmd command calls and frequently used commands


A, Cmd command to call the method

1, the static method call 


   
   
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. // Todo :打开记事本
  6. Process.Start( "notepad");
  7. // Todo :打开路径
  8. Process.Start( @"E:\test");
  9. // Todo :打开文件
  10. Process.Start( @"E:\test\test.txt");
  11. Console.Read();
  12. }
  13. }

2, the package calls


   
   
  1. /// <summary> 执行DOS命令的扩展方法 </summary>
  2. public static class CmdAPI
  3. {
  4. /// <summary> 运行DOS命令 DOS关闭进程命令(ntsd -c q -p PID )PID为进程的ID </summary>
  5. public static string RunCmdOutPut(this string command, EventHandler endEvent = null)
  6. {
  7. // 啟動一個獨立進程
  8. System.Diagnostics.Process p = new System.Diagnostics.Process();
  9. p.StartInfo.FileName = "cmd.exe";
  10. p.StartInfo.Arguments = "/c " + command;
  11. p.StartInfo.UseShellExecute = false;
  12. p.StartInfo.RedirectStandardInput = true;
  13. p.StartInfo.RedirectStandardOutput = true;
  14. p.StartInfo.RedirectStandardError = true;
  15. p.StartInfo.CreateNoWindow = true;
  16. if (endEvent != null)
  17. {
  18. p.EnableRaisingEvents = true;
  19. p.Exited += endEvent;
  20. }
  21. // Todo 2016-11-19 :從輸出流取得命令執行結果
  22. p.Start();
  23. // Todo :不过要记得加上Exit要不然下一行程式执行的时候会当机
  24. p.StandardInput.WriteLine( "exit");
  25. // 從輸出流取得命令執行結果
  26. return p.StandardOutput.ReadToEnd();
  27. Process.Start( "notepad");
  28. }
  29. /// <summary> 运行DOS命令 DOS关闭进程命令(ntsd -c q -p PID )PID为进程的ID </summary>
  30. public static void RunCmd(string command, EventHandler endEvent = null)
  31. {
  32. // 啟動一個獨立進程
  33. System.Diagnostics.Process p = new System.Diagnostics.Process();
  34. p.StartInfo.FileName = "cmd.exe";
  35. p.StartInfo.Arguments = "/c " + command;
  36. p.StartInfo.UseShellExecute = false;
  37. p.StartInfo.RedirectStandardInput = false;
  38. p.StartInfo.RedirectStandardOutput = false;
  39. p.StartInfo.RedirectStandardError = false;
  40. p.StartInfo.CreateNoWindow = false;
  41. if (endEvent != null)
  42. {
  43. p.EnableRaisingEvents = true;
  44. p.Exited += endEvent;
  45. }
  46. p.Start();
  47. }
  48. /// <summary> 关掉进程 P1 进程的PID </summary>
  49. [ Obsolete("未测试")]
  50. public static string CloseProcessByPid(this string pid)
  51. {
  52. return string.Format(CmdStr.CloseProcessByPid, pid).RunCmdOutPut();
  53. }
  54. /// <summary> 执行eclipse程序 </summary>
  55. public static string CmdEclipseByData(this string dataFullPath)
  56. {
  57. return string.Format(CmdStr.CmdEclipseRun, dataFullPath).RunCmdOutPut();
  58. }
  59. }


Second, the commonly used commands Cmd


   
   
  1. class CmdStr
  2. {
  3. /// <summary> DOS关闭进程命令(ntsd -c q -p PID )PID为进程的ID </summary>
  4. public const string CloseProcessByPid = "ntsd -c q -p {0}";
  5. /// <summary> D调用eclipse(eclrun eclipse) </summary>
  6. public const string CmdEclipseRun = "eclrun eclipse {0}";
  7. /// <summary> 查看本机网卡配置信息 "/c ipconfig /all" </summary>
  8. public const string CmdIpConfigerAll = "/c ipconfig /all";
  9. /// <summary> 定时关机 string.Format("/c shutdown -s -t {0}", shijian) </summary>
  10. public const string CmdShutDown = "/c shutdown -s -t {0}";
  11. /// <summary> 取消定时关机 "/c shutdown -a" </summary>
  12. public const string CmdClearShutDown = "/c shutdown -a";
  13. /// <summary> 解析域名ip地址 "/c ping {0}" </summary>
  14. public const string CmdPing= "/c ping {0}";
  15. /// <summary> 显示所有连接和侦听端口 "/c netstat -an" </summary>
  16. public const string CmdNetStat = "/c netstat -an";
  17. /// <summary> 显示路由表内容 "/c netstat -r" </summary>
  18. public const string CmdNetStat_R = "/c netstat -r";
  19. /// <summary> 查询本机系统 "/c winver" </summary>
  20. public const string CmdWinver = "/c winver";
  21. /// <summary> IP地址侦测器 "/c Nslookup" </summary>
  22. public const string CmdNslookup = "/c Nslookup";
  23. /// <summary> 打开磁盘清理工具 "/c cleanmgr" </summary>
  24. public const string CmdCleanmgr = "/c cleanmgr";
  25. /// <summary> 打开系统的注册表 "/c regedit" </summary>
  26. public const string CmdRegedit = "/c regedit";
  27. }



Published 73 original articles · won praise 30 · views 60000 +

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12073132.html