Reputation: 415
I am trying to get the standard out from cmd.exe (testing "DIR" command), and put it into a text box. However, whenever I start the process the program hangs (no button presses).
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();
}
I have also tried Arguments = "DIR"
in the StartInfo block instead of WriteLine.
How do I properly send a command to cmd.exe, without it hanging?
Upvotes: 1
Views: 790
Reputation:
Here you go:
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;
}
Upvotes: 1