Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write to a program's StdOut Stream directly in C#?

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.

like image 945
smwikipedia Avatar asked Feb 27 '10 04:02

smwikipedia


2 Answers

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:

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetStdHandle(int nStdHandle, IntPtr nHandle);

const int STD_OUTPUT_HANDLE = -11;

static void RedirectStandardOutput(FileStream file)
{
    SetStdHandle(STD_OUTPUT_HANDLE, file.SafeFileHandle.DangerousGetHandle());
}
like image 112
MikeP Avatar answered Sep 30 '22 15:09

MikeP


You probably shouldn't set the stdout of your own process. The reason it's stdout is so that the caller (command line, whatever) can redirect it wherever it needs to, without your program having to know.

That's how things like "type foo.txt | more" work. If the type command felt free to redefine stdout to a file, that wouldn't work at all.

If you want to write to a stream, just open the stream and write to it.

like image 37
kyoryu Avatar answered Sep 30 '22 14:09

kyoryu