在 C# 挂起中使用进程调用 cmd.exe
Calling cmd.exe using Process in C# hanging
我正在尝试从 cmd.exe 中获取标准(测试 "DIR" 命令),并将其放入文本框中。但是,每当我启动该程序时,程序就会挂起(没有按下按钮)。
private void cmd_test()
{
Process pr = new Process()
{
StartInfo = {
FileName = "cmd.exe",
UseShellExecute = true,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
}
};
pr.Start();
pr.StandardInput.WriteLine("DIR");
TextBox1.Text = pr.StandardOutput.ReadToEnd();
}
我也试过 Arguments = "DIR"
在 StartInfo 块中而不是 WriteLine。
如何正确地向 cmd.exe 发送命令而不挂起?
给你:
using (var p = new Process())
{
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/C dir";
p.Start();
var output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
richTextBox1.Text = output;
}
我正在尝试从 cmd.exe 中获取标准(测试 "DIR" 命令),并将其放入文本框中。但是,每当我启动该程序时,程序就会挂起(没有按下按钮)。
private void cmd_test()
{
Process pr = new Process()
{
StartInfo = {
FileName = "cmd.exe",
UseShellExecute = true,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
}
};
pr.Start();
pr.StandardInput.WriteLine("DIR");
TextBox1.Text = pr.StandardOutput.ReadToEnd();
}
我也试过 Arguments = "DIR"
在 StartInfo 块中而不是 WriteLine。
如何正确地向 cmd.exe 发送命令而不挂起?
给你:
using (var p = new Process())
{
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/C dir";
p.Start();
var output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
richTextBox1.Text = output;
}