Iam trying to redirect Console.Out to two text files using Console.SetOut.
Console.SetOut(File.CreateText("c:\\del1.txt"));
Console.WriteLine(string1);
...
Console.SetOut(File.CreateText("c:\\del2.txt"));
Console.WriteLine(string2);
By this redirection two text files are created with out any data. This works fine if i comment out second redirection. How can i redirect output to different files using Console.SetOut.
Edit1: Program terminates with out any errors, will this guarantee that all file streams are closed and flushed?
Edit2:
Thanks to every one who has replied to my question, i was able to find solution with out changing code and adding two extra lines to close the file stream.
Console.Out.Close();
Can any one explain why file streams are not closed and flushed after program terminates?
That is a very bizarre scenario – IMO you should just have two different
TextWriter, and write to them when you need to. Leave the console alone. If the caller wants to pipe stdout, let them worry about that via the command shell.and if that means changing some existing code to pass in a
TextWriter, then that is purely a good thing.