C#窗体程序后台执行cmd命令方法
在C#编写的WinFrom窗体中后台执行一个CMD命令,只要传入想要执行的CMD命令即可开始运行,切接不要传入一些不会停止的命令,比如 ping 114.114.114.114 -t 这种会一直执行下去的命令,这样的命令会导致窗体程序卡死无响应。
private string RunCmd(string command)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c " + command;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(command);
p.StandardInput.WriteLine("exit");
return p.StandardOutput.ReadToEnd();
}
上一篇:C#从HTML代码中提取文本内容
下一篇:.Net利用EF连接MySql方法