1、執行單條cmd命令public static string ExecuteCmd(string command) { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute ...
1、執行單條cmd命令
public static string ExecuteCmd(string command) { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; //是否使用操作系統shell啟動 p.StartInfo.RedirectStandardInput = true;//接受來自調用程式的輸入信息 p.StartInfo.RedirectStandardOutput = true;//由調用程式獲取輸出信息 p.StartInfo.RedirectStandardError = true;//重定向標準錯誤輸出 p.StartInfo.CreateNoWindow = true;//不顯示程式視窗 //p.StartInfo.Arguments = "/c " + command;///c代表執行命令後關閉cmd.exe /k參數則不關閉 p.Start();//啟動程式 //消除三行啟動信息 p.StandardOutput.ReadLine(); p.StandardOutput.ReadLine(); p.StandardOutput.ReadLine(); //向cmd視窗發送輸入信息 p.StandardInput.WriteLine(command); p.StandardInput.WriteLine("exit"); string result = p.StandardOutput.ReadToEnd().Replace(Environment.CurrentDirectory, ""); string error = p.StandardError.ReadToEnd(); if (!string.IsNullOrWhiteSpace(error)) result = result + "\r\n<Error Message>:\r\n" + error; p.WaitForExit(); p.Close(); return result; }
2、一次執行多條cmd命令
public static string ExecuteCmd(string[] commands) { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; //是否使用操作系統shell啟動 p.StartInfo.RedirectStandardInput = true;//接受來自調用程式的輸入信息 p.StartInfo.RedirectStandardOutput = true;//由調用程式獲取輸出信息 p.StartInfo.RedirectStandardError = true;//重定向標準錯誤輸出 p.StartInfo.CreateNoWindow = true;//不顯示程式視窗 //p.StandardInput.AutoFlush = true;//每次寫入命令後刷新,報未StandardInput未重定向 //p.StartInfo.Arguments = "/c " + command;///c代表執行命令後關閉cmd.exe /k參數則不關閉 p.Start();//啟動程式 //消除三行啟動信息 p.StandardOutput.ReadLine(); p.StandardOutput.ReadLine(); p.StandardOutput.ReadLine(); //向cmd視窗發送輸入信息 foreach (string cmd in commands) { p.StandardInput.WriteLine(cmd); p.StandardInput.Flush(); } p.StandardInput.WriteLine("exit"); string result = p.StandardOutput.ReadToEnd().Replace(Environment.CurrentDirectory, ""); string error = p.StandardError.ReadToEnd(); if (!string.IsNullOrWhiteSpace(error)) result = result + "\r\n<Error Message>:\r\n" + error; p.WaitForExit(); p.Close(); return result; }
3、利用cmd重定向註冊DLL、創建服務、載入證書
public static string RegsvrFile(string filePath, bool reg = true, bool fileBit64 = false) { string result; string[] commands = { "", "" }; if (!fileBit64 && Environment.Is64BitOperatingSystem) commands[0] = "cd /d %WinDir%\\SysWOW64"; if (reg) commands[1] = "regsvr32 /s \"" + filePath + "\""; else commands[1] = "regsvr32 /u /s \"" + filePath + "\""; if (string.IsNullOrWhiteSpace(commands[0])) result = CmdOper.ExecuteCmd(commands[1]); else result = CmdOper.ExecuteCmd(commands); return result; } public static string ScCreate(string filePath, string serviceName, string displayName, string description, string start="auto", string depend=null) { string result; string[] commands = { "", "" }; if (string.IsNullOrWhiteSpace(depend)) { commands[0] = string.Format("sc create {0} binPath= \"{1}\" type= share start= {2} error= ignore DisplayName= \"{3}\"", serviceName, filePath, start, displayName); } else { commands[0] = string.Format("sc create {0} binPath= \"{1}\" type= share start= {2} error= ignore DisplayName= \"{3}\" depend= {4}", serviceName, filePath, start, displayName, depend); } if (!string.IsNullOrWhiteSpace(description)) { commands[1] = string.Format("sc description {0} \"{1}\"", serviceName, description); } if (string.IsNullOrWhiteSpace(commands[1])) result = CmdOper.ExecuteCmd(commands[0]); else result = CmdOper.ExecuteCmd(commands); return result; } public static string ScStart(string serviceName) { string command = "net start " + serviceName; return CmdOper.ExecuteCmd(command); } public static string ScStop(string serviceName) { string command = "net stop " + serviceName; return CmdOper.ExecuteCmd(command); } public static string ScDelete(string serviceName) { string[] commands = { "net stop " + serviceName, "sc delete " + serviceName }; return CmdOper.ExecuteCmd(commands); } public static string CertFile(string filePath, bool install=true) { string result; string[] commands = { "", "" }; if (Environment.Is64BitOperatingSystem) commands[0] = "cd /d %WinDir%\\SysWOW64"; if (install) commands[1] = "certutil -addstore root \"" + filePath + "\""; else commands[1] = "certutil -delstore root \"" + filePath + "\""; if (string.IsNullOrWhiteSpace(commands[0])) result = CmdOper.ExecuteCmd(commands[1]); else result = CmdOper.ExecuteCmd(commands); return result; }