6

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

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Gjorgji
  • 22,458
  • 10
  • 31
  • 39

3 Answers3

18

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.

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
4

Use Console.SetOut ();

Sorantis
  • 14,496
  • 5
  • 31
  • 37
-1
string consoleString = Console.ReadLine();
Kevin Crowell
  • 10,082
  • 4
  • 35
  • 51