Cmd重定向
Posted publiter
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cmd重定向相关的知识,希望对你有一定的参考价值。
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 + " <Error Message>: " + 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 + " <Error Message>: " + 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; }
以上是关于Cmd重定向的主要内容,如果未能解决你的问题,请参考以下文章