title: C#: get external shell command result line by line
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>What I want is the app receives the cmdline output in real-time, i.e. by lines not by block. Is this doable?</p>
  
  <p>I am using this code to read the output:
      while ((result = proc.StandardOutput.ReadLine()) != null)</p>
  
  <p>But it does not work the way I expected. 
  Thanks in advance.</p>
  
  <p>EDIT: here is the code I am using:</p>
  
  <pre><code>    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.
  </code></pre>