Mega Code Archive

 
Categories / C# / File Stream
 

File AppendAllText Method Appends the string to the file, creating the file if it does not exist

using System; using System.IO; using System.Text; class Test {     public static void Main()     {         string path = @"c:\MyTest.txt";         if (!File.Exists(path))         {             string createText = "Hello " + Environment.NewLine;             File.WriteAllText(path, createText, Encoding.UTF8);         }         string appendText = "extra text" + Environment.NewLine;         File.AppendAllText(path, appendText, Encoding.UTF8);         string readText = File.ReadAllText(path);         Console.WriteLine(readText);     } }