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");