How to read a text file in C#?

This code snippet shows how to read a text file in C#. You must change the path in StreamReader below to your correct file. 

Simply create a console application and copy and paste this code below.

using
System;
using System.IO;

class ReadTextFileSample
{
public static void Main()
{
try
{
using (StreamReader sr = new StreamReader(@"C:\MyTextFile.txt"))
{
String line;

while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);

}
}
}