Writing texts to a file.

Here are what I use to write texts to a file. First way:
using (StreamWriter oStreamWriter = new StreamWriter(outputFilename))
Second way:
using (FileStream oFileStream = new FileStream("Errorlog.txt", FileMode.Append))
  using (TextWriter oTextWriter = new StreamWriter(oFileStream))
    oTextWriter.WriteLine("DateTime: " + DateTime.Now.ToString());
    oTextWriter.WriteLine("Message: " + ex.Message);
    oTextWriter.WriteLine("Source: " + ex.Source);
How to choose the correct FileMode? Take a look of this graph:
This graphic is scaned from the book C# 3.0 in a Nutshell, 3rd Edition. This is a very good C# book, and I will say "Go and grab one".

No comments:

Post a Comment