Skip to content

Instantly share code, notes, and snippets.

Created July 3, 2014 08:26
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Redirect Console.Out to a file.
// Redirection Utility
// Author: Hai Vu (haivu2004 on Google mail)
using System;
using System.IO;
namespace RedirectIO
/// <summary>
/// OutToFile is an easy way to redirect console output to a file.
/// Usage:
/// Console.WriteLine("This text goes to the console by default");
/// using (OutToFile redir = new OutToFile("out.txt"))
/// {
/// Console.WriteLine("Contents of out.txt");
/// }
/// Console.WriteLine("This text goes to console again");
/// </summary>
public class OutToFile : IDisposable
private StreamWriter fileOutput;
private TextWriter oldOutput;
/// <summary>
/// Create a new object to redirect the output
/// </summary>
/// <param name="outFileName">
/// The name of the file to capture console output
/// </param>
public OutToFile(string outFileName)
oldOutput = Console.Out;
fileOutput = new StreamWriter(
new FileStream(outFileName, FileMode.Create)
fileOutput.AutoFlush = true;
// Dispose() is called automatically when the object
// goes out of scope
public void Dispose()
Console.SetOut(oldOutput); // Restore the console output
fileOutput.Close(); // Done with the file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment