Mega Code Archive

 
Categories / C# / File Stream
 

File AppendText Creates a StreamWriter that appends UTF-8 encoded text to an existing file

using System; using System.IO; class Test  {     public static void Main()      {         string path = @"c:\temp\MyTest.txt";         if (!File.Exists(path))          {             using (StreamWriter sw = File.CreateText(path))              {                 sw.WriteLine("Hello");                 sw.WriteLine("And");                 sw.WriteLine("Welcome");             }           }         using (StreamWriter sw = File.AppendText(path))          {             sw.WriteLine("This");             sw.WriteLine("is Extra");             sw.WriteLine("Text");         }           using (StreamReader sr = File.OpenText(path))          {             string s = "";             while ((s = sr.ReadLine()) != null)              {                 Console.WriteLine(s);             }         }     } }