1

Using c#, is there anyway to copy console output to a second location (aswell as the original console). I know i can call SetOut to override the default output location of the console, but what i want to do, is keep writing to the original console implementation, but also write to a second location. Any ideas?

richzilla
  • 38,000
  • 13
  • 54
  • 85

1 Answers1

0

you can try with this code

FileStream ostrm;
StreamWriter writer;
TextWriter oldOut = Console.Out;

try
{
    ostrm = new FileStream ("./Redirect.txt", FileMode.OpenOrCreate, FileAccess.Write);
    writer = new StreamWriter (ostrm);
}
catch (Exception e)
{
    Console.WriteLine ("Cannot open Redirect.txt for writing");
    Console.WriteLine (e.Message);
    return;
}
Console.SetOut (writer);
Console.WriteLine ("This is a line of text");
Console.SetOut (oldOut);
writer.Close();
ostrm.Close();
Console.WriteLine ("Done");
LordAro
  • 1,219
  • 3
  • 16
  • 31
Aghilas Yakoub
  • 27,871
  • 5
  • 44
  • 49