Get Command Prompt Window Content After Command Completion

c#, cmd, winforms

Solution

This should do it

ProcessStartInfo info = new ProcessStartInfo(); 
info.Arguments = "/C ping 127.0.0.1"; 
info.WindowStyle = ProcessWindowStyle.Hidden; 
info.CreateNoWindow = true; 
info.FileName = "cmd.exe"; 
info.UseShellExecute = false; 
info.RedirectStandardOutput = true; 
using (Process process = Process.Start(info)) 
{ 
    using (StreamReader reader = process.StandardOutput) 
    { 
        string result = reader.ReadToEnd(); 
        textBox1.Text += result; 
    } 
} 

Output

Problem

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.

Original source