sarsnake sarsnake - 1 month ago 16
C# Question

StreamWriter.Write doesn't write to file; no exception thrown

My code in C# (asp.net MVC)

StreamWriter tw = new StreamWriter("C:\\mycode\\myapp\\logs\\log.txt");

// write a line of text to the file
tw.Write("test");


The file is created but is empty. No exception is thrown. I have never seen this before and I am stuck here; I just need to write some debugging output.

Please advise.

Answer

Use

System.IO.File.WriteAllText(@"path\te.txt", "text");