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
  • 40,440
  • 14
  • 56
  • 86

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,269
  • 3
  • 18
  • 35
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51