Redirect the output of one exe to another exe: C #

I created two simple .exe files. One of them takes a file name parameter at runtime and reads the contents of the file to the console. Another awaits its console input, and then reads it; for now, he just needs to print to the console, but in the end I will have to redirect the reading to text in a new txt file. My question is, how can I redirect the output of the first exe to the second exe console, where can it be read?

Thanks in advance for any help you can provide! :)

-Chris

+4
source share
2 answers

Perhaps you can do something with the command line using the pipe redirection operator:

ConsoleApp1.exe | ConsoleApp2.exe 

The pipe statement redirects console output from the first application to the standard input of the second application. You can find more information here (the link is for XP, but these rules apply to Windows Vista and Windows 7).

+4
source

From MSDN

 // Start the child process. Process p = new Process(); // Redirect the output stream of the child process. p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.FileName = "Write500Lines.exe"; p.Start(); // Do not wait for the child process to exit before // reading to the end of its redirected stream. // p.WaitForExit(); // Read the output stream first and then wait. string output = p.StandardOutput.ReadToEnd(); p.WaitForExit(); 

you can get line by line, and instead of:

 ///... string output; while( ( output = p.StandardOutput.ReadLine() ) != null ) { Console.WriteLine(output); } p.WaitForExit(); 
+2
source

All Articles