0

I was doing some trials on the basis of the following Q&A: Where does Console.WriteLine go in ASP.NET?.

The code I tried goes like below:

    var fs = new System.IO.FileStream(@"D:\log.txt", System.IO.FileMode.Append);
    var tr = new System.IO.StreamWriter(fs);
    Console.SetOut(tr);
    Console.WriteLine("My Default Debugging");
    fs.Close();

Here I am setting the FileStream fs to StreamWriter tr and in turn setting it as Console.Out by calling Console.SetOut(). So, by that I am expecting it to write to the file by Console.WriteLine(). Though my file gets created, it is empty.

What can be the thing I am missing here?

4

3 回答 3

1

试试吧tr.WriteLine("string");

于 2013-09-19T10:37:20.603 回答
0
var fs = new System.IO.FileStream(@"D:\log.txt", System.IO.FileMode.Append);
var tr = new System.IO.StreamWriter(fs);
Console.SetOut(tr);
Console.WriteLine("My Default Debugging");
tr.Close();
fs.Close();

也许是因为您在关闭 FileStream 之前没有关闭 StreamWriter?

于 2013-09-19T10:45:30.820 回答
0

因为Console.WriteLine()不这样做?

将指定的数据(后跟当前行终止符)写入标准输出流。

于 2013-09-19T10:35:19.977 回答