C#: get external shell command result line by line

10,019

Solution 1

Have a look at the example in this msdn article on how to do the reading completly async.

Beyond that I expect your code does to read line by line now but the UI doesn't get any time to repaint (missing Application.DoEvents(); after updating the RTFTextBox

Solution 2

Instead of loop using while ((result = proc.StandardOutput.ReadLine()) != null) you should of using:

...
proc.OutputDataReceived += proc_DataReceived;
proc.Start();
proc.BeginOutputReadLine();
proc.WaitForExit();

This will start asynchronous reading the lines when they arrives, you then handle the lines read by e.Data in proc_DataReceived handler, since you are use BeginOutputReadline the e.Data will be a string lines.

Solution 3

This could be usefull:

http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8d6cebfc-9b8b-4667-85b5-2b92105cd0b7/

http://www.dotnetperls.com/redirectstandardoutput

Share:
10,019
Charlie
Author by

Charlie

Updated on June 25, 2022

Comments

  • Charlie
    Charlie almost 2 years

    I am writing a C# winform application that starts a second process to execute shell commands like "dir" and "ping". I redirect the second process's output so my app can receive the command result. It roughly works fine.

    The only problem is my winform app receives the command line output as a whole instead of line by line. For example, it has to wait for the external "ping" command to finish (which takes many seconds or longer) and then receives the whole output (many lines) at once.

    What I want is the app receives the cmdline output in real-time, i.e. by lines not by block. Is this doable?

    I am using this code to read the output: while ((result = proc.StandardOutput.ReadLine()) != null)

    But it does not work the way I expected. Thanks in advance.

    EDIT: here is the code I am using:

        System.Diagnostics.ProcessStartInfo procStartInfo = new 
                System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
    
        procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    
        // The following commands are needed to redirect the standard output.
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.UseShellExecute = false;
        procStartInfo.CreateNoWindow = true;
        // Now we create a process, assign its ProcessStartInfo and start it
        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.StartInfo = procStartInfo;
        proc.Start();
    
        // Get the output into a string
        string result;
        try {
            while ((result = proc.StandardOutput.ReadLine()) != null)
            {
                AppendRtfText(result+"\n", Brushes.Black);
            }
        } // here I expect it to update the text box line by line in real time
          // but it does not.
    
  • Charlie
    Charlie over 12 years
    Thanks @Eddy, you are right. But DoEvents() is not available in WPF. I used this solution: stackoverflow.com/questions/4502037/…