Winform执行CMD命令

1、首先分享CmdHelper类

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.Diagnostics;
  5 
  6 namespace Helper
  7 {
  8     /// <summary>
  9     /// 执行命令
 10     /// </summary>
 11     public class CmdHelper
 12     {
 13         /// 
 14         /// 执行cmd.exe命令
 15         /// 
 16         ///命令文本
 17         /// 命令输出文本
 18         public static string ExeCommand(string commandText)
 19         {
 20             return ExeCommand(new string[] { commandText });
 21         }
 22         /// 
 23         /// 执行多条cmd.exe命令
 24         /// 
 25         ///命令文本数组
 26         /// 命令输出文本
 27         public static string ExeCommand(string[] commandTexts)
 28         {
 29             Process p = new Process();
 30             p.StartInfo.FileName = "cmd.exe";
 31             p.StartInfo.UseShellExecute = false;
 32             p.StartInfo.RedirectStandardInput = true;
 33             p.StartInfo.RedirectStandardOutput = true;
 34             p.StartInfo.RedirectStandardError = true;
 35             p.StartInfo.CreateNoWindow = true;
 36             string strOutput = null;
 37             try
 38             {
 39                 p.Start();
 40                 foreach (string item in commandTexts)
 41                 {
 42                     p.StandardInput.WriteLine(item);
 43                 }
 44                 p.StandardInput.WriteLine("exit");
 45                 strOutput = p.StandardOutput.ReadToEnd();
 46                 //strOutput = Encoding.UTF8.GetString(Encoding.Default.GetBytes(strOutput));
 47                 p.WaitForExit();
 48                 p.Close();
 49             }
 50             catch (Exception e)
 51             {
 52                 strOutput = e.Message;
 53             }
 54             return strOutput;
 55         }
 56         /// 
 57         /// 启动外部Windows应用程序,隐藏程序界面
 58         /// 
 59         ///应用程序路径名称
 60         /// true表示成功,false表示失败
 61         public static bool StartApp(string appName)
 62         {
 63             return StartApp(appName, ProcessWindowStyle.Hidden);
 64         }
 65         /// 
 66         /// 启动外部应用程序
 67         /// 
 68         ///应用程序路径名称
 69         ///进程窗口模式
 70         /// true表示成功,false表示失败
 71         public static bool StartApp(string appName, ProcessWindowStyle style)
 72         {
 73             return StartApp(appName, null, style);
 74         }
 75         /// 
 76         /// 启动外部应用程序,隐藏程序界面
 77         /// 
 78         ///应用程序路径名称
 79         ///启动参数
 80         /// true表示成功,false表示失败
 81         public static bool StartApp(string appName, string arguments)
 82         {
 83             return StartApp(appName, arguments, ProcessWindowStyle.Hidden);
 84         }
 85         /// 
 86         /// 启动外部应用程序
 87         /// 
 88         ///应用程序路径名称
 89         ///启动参数
 90         ///进程窗口模式
 91         /// true表示成功,false表示失败
 92         public static bool StartApp(string appName, string arguments, ProcessWindowStyle style)
 93         {
 94             bool blnRst = false;
 95             Process p = new Process();
 96             p.StartInfo.FileName = appName;//exe,bat and so on
 97             p.StartInfo.WindowStyle = style;
 98             p.StartInfo.Arguments = arguments;
 99             try
100             {
101                 p.Start();
102                 p.WaitForExit();
103                 p.Close();
104                 blnRst = true;
105             }
106             catch
107             {
108             }
109             return blnRst;
110         }
111 
112        /// <summary>
113        /// 实现压缩,需要rar.exe上传到网站根目录
114        /// </summary>
115        /// <param name="s"></param>
116        /// <param name="d"></param>
117         /// <example>rar("e:/www.svnhost.cn/", "e:/www.svnhost.cn.rar");</example>
118         public static void Rar(string s, string d)
119         {
120             ExeCommand(System.Web.HttpContext.Current.Server.MapPath("~/rar.exe") + " a \"" + d + "\" \"" + s + "\" -ep1");
121         }
122 
123         /// <summary>
124         /// 实现解压缩,需要rar.exe上传到网站根目录
125         /// </summary>
126         /// <param name="s"></param>
127         /// <param name="d"></param>
128         /// <example>unrar("e:/www.svnhost.cn.rar", "e:/");</example>
129         public static void UnRar(string s, string d)
130         {
131             ExeCommand(System.Web.HttpContext.Current.Server.MapPath("~/rar.exe") + " x \"" + s + "\" \"" + d + "\" -o+");
132         }
133 
134     }
135 
136 }

2、利用CmdHelper类执行操作

 (1)检查Winddows激活有效期:

CmdHelper.ExeCommand("slmgr.vbs -xpr");

(2)计算器

CmdHelper.ExeCommand("calc");

(3)记事本

CmdHelper.ExeCommand("notepad");

(4)注册表编辑器

CmdHelper.ExeCommand("regedit");

(5)计算机性能监测

CmdHelper.ExeCommand("perfmon.msc");

(6)任务管理器

CmdHelper.ExeCommand("taskmgr");

(7)系统版本

CmdHelper.ExeCommand("winver");

(8)画图板

CmdHelper.ExeCommand("mspaint");

(9)远程桌面连接

CmdHelper.ExeCommand("mstsc");

(10)放大镜

CmdHelper.ExeCommand("magnify");

(11)DirectX诊断工具

CmdHelper.ExeCommand("dxdiag");

(12)屏幕键盘

CmdHelper.ExeCommand("osk");

(13)事件查看器

CmdHelper.ExeCommand("eventvwr");

(14)造字程序

CmdHelper.ExeCommand("eudcedit");

(15)字符映射表

CmdHelper.ExeCommand("charmap");

(16)注销

CmdHelper.ExeCommand("logoff");

(17)关机

CmdHelper.ExeCommand("shutdown -s -t 00");

(18)60s后关机

CmdHelper.ExeCommand("shutdown -s -t 60");

(19)重启

CmdHelper.ExeCommand("shutdown -r -t 00");

(20)取消关机/重启指令

CmdHelper.ExeCommand("shutdown -a");

 

posted @ 2019-05-27 09:36  薄心之心  阅读(2581)  评论(0编辑  收藏  举报