How to Redirect Console Output to a File in C#

I encountered the need to redirect the console output to a file in my attempt to automate testing of functions which does not return any value but writes to the console. Basically, we need to save the old output (Console.Out) before assigning the output to a file stream by means of Console.SetOut(). After the test, I also need to restore the console output.

After doing this for a while, it gets old and I told myself, “Self, I can create a simple class to simplify this task.” In my vision, output redirect should be easy, simple, and practical. The user should not have to remember the details such as which data type to use (TextWriter, FileStream, StreamWriter, …), or which steps to take. Thus, I created OutToFile, a simple class which encapsulate this idea. Using OutToFile is … you guessed it, easy:

Console.WriteLine(“This goes to console”);

using (new OutToFile(“output.txt”)) // redirect the output to a text file
    Console.WriteLine(“This text goes to a file”);
} // redirection ends upon exiting the using block

Console.WriteLine(“This goes back to console”);

The source code for this class is available, you are free to use it, but I reserve the bragging right🙂.

Blogged with Flock

Tags: , ,

8 thoughts on “How to Redirect Console Output to a File in C#

  1. Eliot

    This is exactly what I was looking for! We have tons of console print debugging statements in our XNA game engine: With this class I believe we can catch all of them and redirect them to a file.
    Thanks for allowing us to use this code. If we wind up using it it will be noted that it is not under the project’s Apache 2.0 license.

  2. Edgar

    Hello Hai,

    Do you know how I may be able to output this process to a txt or log file?

    Process ps = new Process();
    ps = Process.Start(NUNIT_CONSOLE, path + “\\” + nunitFile + ” /include:\”” + sTestConcepts + “\”” + “/out:C:/Users/Edgar/Documents/TestResults.txt” );
    //wait until the process is done

    while (!ps.HasExited) System.Threading.Thread.Sleep(100);
    testConcepts = new ArrayList();

    Thank you in advance for your help!


  3. Edgar


    Thank you for your help and quick response! The link you provided was very helpful. I ended up using a stream writer and it works now. I am having trouble with the console freezing while running (it is running multiple processes) but this is a different issue.

    I appreciate the help!


  4. Pingback: Problems in C# and Solutions | Radin Ahmed Ehsan

  5. Thank you!

    It’s works for me.
    I needed to read the output from a command in c# to send it for a file, than use a external bat file to read it’s contents and comparate the string readed with another txt file.
    Thank you!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s