Как записать данные в файл?

Простой способ для сохранения ошибок в виде текста, когда нельзя прерывать программу, записать их в текстовый файл.

#region WriteLogError(string aError)
public static void WriteLogError(string aError)
{
    // create a writer and open the file
    FileInfo fi = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + @"\Error.txt");
     TextWriter tw;

     if (fi.Exists)
    {
       tw = fi.AppendText();
    }
    else
    {
        tw = fi.CreateText();
    }

    // write a line of text to the file
    tw.WriteLine(aError);

    // close the stream
    tw.Close();
}
#endregion

а так можно сохранить в виде XML

Currently rated 3.7 by 3 people

  • Currently 3.666667/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Comments

Add comment


 

biuquote
  • Comment
  • Preview
Loading