C#执行cmd窗口 隐藏和显示 两种模式
隐藏 CMD 窗口,但捕获输出并显示
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; //显示隐藏窗口
p.Start();
p.StandardInput.WriteLine("ping 10.18.3.2"); //填CMD命令
//p.StandardInput.WriteLine("exit ");
//string strRst = p.StandardOutput.ReadToEnd();
//MessageBox.Show(strRst); /*消息弹出查看*/
p.WaitForExit();
p.Close();
/c:执行完命令后关闭 CMD。- 捕获输出并显示在
MessageBox中。
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c ping 10.18.3.2"; // /c 表示执行完命令后关闭
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true; // 隐藏窗口
p.Start();
// 读取输出
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
// 显示输出
MessageBox.Show("输出内容:\n" + output + "\n错误信息:\n" + error);
显示 CMD 窗口并保留输出
显示 CMD 并保留输出 Arguments = "/k ping xxx",UseShellExecute = true CMD 不会关闭,能看到输出
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/k ping 10.18.3.2"; // /k 表示执行完命令后保持窗口
p.StartInfo.UseShellExecute = true; // 必须为 true 才能显示窗口
p.StartInfo.RedirectStandardInput = false;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.RedirectStandardError = false;
p.StartInfo.CreateNoWindow = false;
p.Start();
p.WaitForExit();
cmd怎么同时执行两个命令
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/k \"ping 10.18.3.2 & ping 10.18.3.3\""; // /k 表示执行完命令后保持窗口
p.StartInfo.UseShellExecute = true; // 必须为 true 才能显示窗口
p.StartInfo.RedirectStandardInput = false;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.RedirectStandardError = false;
p.StartInfo.CreateNoWindow = false; //显示窗口
p.Start();
p.WaitForExit();
p.Close();