title: How to write to a program's StdOut Stream directly in C#?
body: <p>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:</p>
  
  <pre><code>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();
  </code></pre>
  
  <p>But this approach is to connect the program's <em>stdout</em> to <em>Console.Out</em> and redirect the <em>Console.Out</em> to the file. It's kind of like this:</p>
  
  <blockquote>
    <p>Program.StdOut  -> Console.Out -> File</p>
  </blockquote>
  
  <p>The Console.Out here seems to be a bridge. I don't want this bridge and I don't want to use <em>Console.Write()</em> to make the output. How could I map the program's stdout stream to the target file <strong><em>directly</em></strong> and write to the program's stdout directly instead of <em>Console.Write()</em>?  What I want is kind of like this:</p>
  
  <blockquote>
    <p>Program.StdOut -> File</p>
  </blockquote>
  
  <p>The <em>Process.StandardOutput</em> property only gives me a readonly <em>StreamReader</em> object. How to write to the Process.StandardOutput?? Or where is the <em>program's stdout</em>?</p>
  
  <p>Many thanks.</p>