I know that there’re stdout/in/err for a program and I want to redirect a program’s output to a file instead of the console output. And I now figure it out with the code below:
FileStream fs = File.Open(@"E:\console.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
TextWriter old = Console.Out;
Console.SetOut(sw);
Console.Write("bbb");
sw.Flush();
But this approach is to connect the program’s stdout to Console.Out and redirect the Console.Out to the file. It’s kind of like this:
Program.StdOut -> Console.Out -> File
The Console.Out here seems to be a bridge. I don’t want this bridge and I don’t want to use Console.Write() to make the output. How could I map the program’s stdout stream to the target file directly and write to the program’s stdout directly instead of Console.Write()? What I want is kind of like this:
Program.StdOut -> File
The Process.StandardOutput property only gives me a readonly StreamReader object. How to write to the Process.StandardOutput?? Or where is the program’s stdout?
Many thanks.
You can get a hold of the application’s stdout by calling Console.OpenStandardOutput. From there, you can do whatever you want with the stream, although you won’t be able to reassign it. If you want to do that you’ll have to P/Invoke SetStdHandle and handle the details yourself.
EDIT: Added example code for the P/Invoke route: