Mega Code Archive

 
Categories / C# / File Stream
 

File ReadAllLines Method Opens a text file, reads all lines of the file, and then closes the file

using System; using System.IO; class Test {     public static void Main()     {         string path = @"c:\temp\MyTest.txt";         if (!File.Exists(path))         {             string[] createText = { "B", "A", "c" };             File.WriteAllLines(path, createText);         }         string appendText = "This is extra text" + Environment.NewLine;         File.AppendAllText(path, appendText);         string[] readText = File.ReadAllLines(path);         foreach (string s in readText)         {             Console.WriteLine(s);         }     } }