I had this code
StreamWriter sw = new StreamWriter(this.FileName, true); sw.WriteLine(text); sw.Close();
Which I changed to this (because of a contention issue):
StreamWriter sw = new StreamWriter( new FileStream(this.FileName, FileMode.OpenOrCreate , FileAccess.ReadWrite, FileShare.Write) , Encoding.ASCII); sw.WriteLine(strLog); sw.Close();
The issue is that the first one worked fine, outputted proper text that was human readable (this is for a log text file). The second one outputs totally screwy output, regardless of the Encoding type I’ve used. I’ve tried ASCII, UTF7, 8, Unicode and Default. So clearly I’m missing something fundamental about FileStream or TextWriter. Please edumacate me.
I’ve tried your code without being able to reproduce any weird behavior.
A few thoughts:
EDIT:
The solution to the problem was to use FileMode.Append, per comments to this post.