Redirecting the console to a string in a .NET program

How can I redirect everything written to the console to write to a string?

+6
string c # console
source share
3 answers

For your own Console.SetOut process and redirect it to a TextWriter created on top of a string builder or memory stream. To start a child process, use ProcessStartInfo.RedirectStandardOutput when starting the process.

+18
source share

Use Console.SetOut ();

+4
source share
 string consoleString = Console.ReadLine(); 
-one
source share

All Articles