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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM