I’m looking to get the content of a command prompt window after the completion of a command in C#.
Specifically, in this instance, I’m issuing a ping command from a button click and want to display the output in a textbox.
The code I’m using currently is:
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.Arguments = "ping 192.168.1.254";
Process pingIt = Process.Start(startInfo);
string errors = pingIt.StandardError.ReadToEnd();
string output = pingIt.StandardOutput.ReadToEnd();
txtLog.Text = "";
if (errors != "")
{
txtLog.Text = errors;
}
else
{
txtLog.Text = output;
}
And it works, sort of. It grabs at least some output and displays it, but the ping itself doesn’t execute – or at least this is what I assume given the output below and the command prompt window flashes for a brief second.
Output:
Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft
Corporation. All rights reserved.C:\Checkout\PingUtility\PingUtility\bin\Debug>
Any assistance is greatly appreciated.
This should do it
Output