
Selectively capturing console output in C#
By:
kruncher on
Feb 10th, 2013 | syntax:
C# | size: 0.38 KB | hits: 17 | expires: Never
// Keep track of the original output writer
TextWriter originalOut = Console.Out;
// Override console output with a custom one
StringWriter buf = new StringWriter();
Console.SetOut(buf);
Console.WriteLine("This will be captured!");
// Capture output!
buf.Flush();
string capturedOutput = buf.ToString();
// Restore original output writer
Console.SetOut(originalOut);