Here is source code to Read the Contents of a File.The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
* C# Program to Read Contents of a File
public void readdata()
FileStream fs = new FileStream("Myfile.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);//Position the File Pointer at the Beginning of the File
sr.BaseStream.Seek(0, SeekOrigin.Begin);//Read till the End of the File is Encountered
string str = sr.ReadLine();
while (str != null)
str = sr.ReadLine();
//Close the Writer and File
public static void Main(String args)
FileRead fr = new FileRead();
Here is the output of the C# Program:
The text which your are reading are read from the file named myfile.txt that is created already.
Sanfoundry Global Education & Learning Series – 1000 C# Programs.