C# : Capturing process output

Created 08 April 2003 00:00
By running external commands using the System.Diagnostics.Process class, you can also retrieve the output from that command by using it's StandardOutput property:

  Process p = new Process();
  StreamReader sr = null;
  ProcessStartInfo psi = new ProcessStartInfo("D:\Matt\test.bat");
  psi.UseShellExecute = false;
  psi.RedirectStandardOutput = true;
  psi.CreateNoWindow = true;
  p.StartInfo = psi;
  p.Start();
  sr = p.StandardOutput;
  string line = null;
  while ((line = sr.ReadLine()) != null) {
    Console.WriteLine(line);
  }