Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting console to a string in a .NET program

Tags:

string

c#

console

How can I redirect whatever is written to the console to be written into a string?

like image 284
Gjorgji Avatar asked Mar 17 '10 23:03

Gjorgji


2 Answers

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

like image 81
Remus Rusanu Avatar answered Oct 19 '22 06:10

Remus Rusanu


Use Console.SetOut ();

like image 26
Sorantis Avatar answered Oct 19 '22 06:10

Sorantis