Winform執行CMD命令

1、首先分享CmdHelper類

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace Helper
{
    /// <summary>
    /// 執行命令
    /// </summary>
    public class CmdHelper
    {
        /// 
        /// 執行cmd.exe命令
        /// 
        ///命令文本
        /// 命令輸出文本
        public static string ExeCommand(string commandText)
        {
            return ExeCommand(new string[] { commandText });
        }
        /// 
        /// 執行多條cmd.exe命令
        /// 
        ///命令文本數組
        /// 命令輸出文本
        public static string ExeCommand(string[] commandTexts)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            string strOutput = null;
            try
            {
                p.Start();
                foreach (string item in commandTexts)
                {
                    p.StandardInput.WriteLine(item);
                }
                p.StandardInput.WriteLine("exit");
                strOutput = p.StandardOutput.ReadToEnd();
                //strOutput = Encoding.UTF8.GetString(Encoding.Default.GetBytes(strOutput));
                p.WaitForExit();
                p.Close();
            }
            catch (Exception e)
            {
                strOutput = e.Message;
            }
            return strOutput;
        }
        /// 
        /// 啓動外部Windows應用程序,隱藏程序界面
        /// 
        ///應用程序路徑名稱
        /// true表示成功,false表示失敗
        public static bool StartApp(string appName)
        {
            return StartApp(appName, ProcessWindowStyle.Hidden);
        }
        /// 
        /// 啓動外部應用程序
        /// 
        ///應用程序路徑名稱
        ///進程窗口模式
        /// true表示成功,false表示失敗
        public static bool StartApp(string appName, ProcessWindowStyle style)
        {
            return StartApp(appName, null, style);
        }
        /// 
        /// 啓動外部應用程序,隱藏程序界面
        /// 
        ///應用程序路徑名稱
        ///啓動參數
        /// true表示成功,false表示失敗
        public static bool StartApp(string appName, string arguments)
        {
            return StartApp(appName, arguments, ProcessWindowStyle.Hidden);
        }
        /// 
        /// 啓動外部應用程序
        /// 
        ///應用程序路徑名稱
        ///啓動參數
        ///進程窗口模式
        /// true表示成功,false表示失敗
        public static bool StartApp(string appName, string arguments, ProcessWindowStyle style)
        {
            bool blnRst = false;
            Process p = new Process();
            p.StartInfo.FileName = appName;//exe,bat and so on
            p.StartInfo.WindowStyle = style;
            p.StartInfo.Arguments = arguments;
            try
            {
                p.Start();
                p.WaitForExit();
                p.Close();
                blnRst = true;
            }
            catch
            {
            }
            return blnRst;
        }

       /// <summary>
       /// 實現壓縮,需要rar.exe上傳到網站根目錄
       /// </summary>
       /// <param name="s"></param>
       /// <param name="d"></param>
        /// <example>rar("e:/www.svnhost.cn/", "e:/www.svnhost.cn.rar");</example>
        public static void Rar(string s, string d)
        {
            ExeCommand(System.Web.HttpContext.Current.Server.MapPath("~/rar.exe") + " a \"" + d + "\" \"" + s + "\" -ep1");
        }

        /// <summary>
        /// 實現解壓縮,需要rar.exe上傳到網站根目錄
        /// </summary>
        /// <param name="s"></param>
        /// <param name="d"></param>
        /// <example>unrar("e:/www.svnhost.cn.rar", "e:/");</example>
        public static void UnRar(string s, string d)
        {
            ExeCommand(System.Web.HttpContext.Current.Server.MapPath("~/rar.exe") + " x \"" + s + "\" \"" + d + "\" -o+");
        }

    }

}

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");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章