Skip to content

Instantly share code, notes, and snippets.

@srasch
Created July 3, 2014 08:26
Show Gist options
  • Save srasch/ea0e475c38d59dba0fb1 to your computer and use it in GitHub Desktop.
Save srasch/ea0e475c38d59dba0fb1 to your computer and use it in GitHub Desktop.
Redirect Console.Out to a file.
// Redirection Utility
// Author: Hai Vu (haivu2004 on Google mail)
// http://wuhrr.wordpress.com/2008/01/05/how-to-redirect-console-output-to-a-file-in-c/
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;
Console.SetOut(fileOutput);
}
// 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