Reading a file with C#

Reading a text file line by line in C#. Dont forget to include using System.IO;

StreamReader myReader = new StreamReader(@"c:\test.txt");
string line = "";
while (line != null)
   line = myReader.ReadLine();

You can read all the text within a file using the ReadAllText function.

string filename = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\test.txt";
string content = File.ReadAllText(filename);

Leave a Reply