一、Cmd命令调用方法
1、静态方法调用
- class Program
- {
- static void Main(string[] args)
- {
- // Todo :打开记事本
- Process.Start("notepad");
- // Todo :打开路径
- Process.Start(@"E:\test");
- // Todo :打开文件
- Process.Start(@"E:\test\test.txt");
- Console.Read();
- }
- }
2、封装调用
- /// <summary> 执行DOS命令的扩展方法 </summary>
- public static class CmdAPI
- {
- /// <summary> 运行DOS命令 DOS关闭进程命令(ntsd -c q -p PID )PID为进程的ID </summary>
- public static string RunCmdOutPut(this string command, EventHandler endEvent = null)
- {
- // 啟動一個獨立進程
- System.Diagnostics.Process p = new System.Diagnostics.Process();
- p.StartInfo.FileName = "cmd.exe";
- p.StartInfo.Arguments = "/c " + command;
- p.StartInfo.UseShellExecute = false;
- p.StartInfo.RedirectStandardInput = true;
- p.StartInfo.RedirectStandardOutput = true;
- p.StartInfo.RedirectStandardError = true;
- p.StartInfo.CreateNoWindow = true;
- if (endEvent != null)
- {
- p.EnableRaisingEvents = true;
- p.Exited += endEvent;
- }
- // Todo 2016-11-19 :從輸出流取得命令執行結果
- p.Start();
- // Todo :不过要记得加上Exit要不然下一行程式执行的时候会当机
- p.StandardInput.WriteLine("exit");
- // 從輸出流取得命令執行結果
- return p.StandardOutput.ReadToEnd();
- Process.Start("notepad");
- }
- /// <summary> 运行DOS命令 DOS关闭进程命令(ntsd -c q -p PID )PID为进程的ID </summary>
- public static void RunCmd(string command, EventHandler endEvent = null)
- {
- // 啟動一個獨立進程
- System.Diagnostics.Process p = new System.Diagnostics.Process();
- p.StartInfo.FileName = "cmd.exe";
- p.StartInfo.Arguments = "/c " + command;
- p.StartInfo.UseShellExecute = false;
- p.StartInfo.RedirectStandardInput = false;
- p.StartInfo.RedirectStandardOutput = false;
- p.StartInfo.RedirectStandardError = false;
- p.StartInfo.CreateNoWindow = false;
- if (endEvent != null)
- {
- p.EnableRaisingEvents = true;
- p.Exited += endEvent;
- }
- p.Start();
- }
- /// <summary> 关掉进程 P1 进程的PID </summary>
- [Obsolete("未测试")]
- public static string CloseProcessByPid(this string pid)
- {
- return string.Format(CmdStr.CloseProcessByPid, pid).RunCmdOutPut();
- }
- /// <summary> 执行eclipse程序 </summary>
- public static string CmdEclipseByData(this string dataFullPath)
- {
- return string.Format(CmdStr.CmdEclipseRun, dataFullPath).RunCmdOutPut();
- }
- }
二、常用的Cmd命令
- class CmdStr
- {
- /// <summary> DOS关闭进程命令(ntsd -c q -p PID )PID为进程的ID </summary>
- public const string CloseProcessByPid = "ntsd -c q -p {0}";
- /// <summary> D调用eclipse(eclrun eclipse) </summary>
- public const string CmdEclipseRun = "eclrun eclipse {0}";
- /// <summary> 查看本机网卡配置信息 "/c ipconfig /all" </summary>
- public const string CmdIpConfigerAll = "/c ipconfig /all";
- /// <summary> 定时关机 string.Format("/c shutdown -s -t {0}", shijian) </summary>
- public const string CmdShutDown = "/c shutdown -s -t {0}";
- /// <summary> 取消定时关机 "/c shutdown -a" </summary>
- public const string CmdClearShutDown = "/c shutdown -a";
- /// <summary> 解析域名ip地址 "/c ping {0}" </summary>
- public const string CmdPing= "/c ping {0}";
- /// <summary> 显示所有连接和侦听端口 "/c netstat -an" </summary>
- public const string CmdNetStat = "/c netstat -an";
- /// <summary> 显示路由表内容 "/c netstat -r" </summary>
- public const string CmdNetStat_R = "/c netstat -r";
- /// <summary> 查询本机系统 "/c winver" </summary>
- public const string CmdWinver = "/c winver";
- /// <summary> IP地址侦测器 "/c Nslookup" </summary>
- public const string CmdNslookup = "/c Nslookup";
- /// <summary> 打开磁盘清理工具 "/c cleanmgr" </summary>
- public const string CmdCleanmgr = "/c cleanmgr";
- /// <summary> 打开系统的注册表 "/c regedit" </summary>
- public const string CmdRegedit = "/c regedit";
- }
发布了73 篇原创文章 · 获赞 30 · 访问量 6万+
原文地址:https://www.cnblogs.com/lonelyxmas/p/12073132.html
时间: 2024-10-07 14:10:00