website/archived/howto_pipeoutput.md

884 B

title lastmodified redirect_from
Howto PipeOutput 2007-10-17
/Howto_PipeOutput/

Howto PipeOutput

This How-To shows how to get the output of a child process.

// This sample runs grep and extracts all the lines that match
// the line "miguel" from /etc/passwd
using System;
using System.Diagnostics;
 
class Test
{
         public static void Main ()
         {
                 ProcessStartInfo psi = new ProcessStartInfo ();
                 psi.FileName = "grep";
                 psi.Arguments = "miguel /etc/passwd";
                 psi.RedirectStandardOutput = true;
                 psi.UseShellExecute = false;
 
                 Process p =  Process.Start (psi);
                 string ret = p.StandardOutput.ReadToEnd ();
                 p.WaitForExit ();
 
                 Console.WriteLine (ret);
         }
}
\