I am trying to redirect Console.Outto 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);
With this redirection, two text files are created without any data. This works fine if I comment on the second redirect. How can I redirect output to different files using Console.SetOut.
Edit1: the program ends without any errors, does it guarantee that all file streams will be closed and reset?
Edit2: Thanks to everyone who answered my question, I was able to find a solution without changing the code and add two additional lines to close the file stream. Console.Out.Close();
Can anyone explain why the file streams are not closed and not cleaned after the program terminates?
source
share