Created
July 3, 2014 08:26
-
-
Save srasch/ea0e475c38d59dba0fb1 to your computer and use it in GitHub Desktop.
Redirect Console.Out to a file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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