Use StreamReader to Read A Text File in .NET

By Robbe Morris

Brief sample demonstrating how to read a text file in all at once.

using System.IO;
using System;
using System.Text;

public string ReadTextFile(string fileName)
{
   using(StreamReader sr = new StreamReader(fileName))
   {
     return sr.ReadToEnd();
   }
}


// In .NET 2.0 and later, you can use the following:

File.ReadAllText(fileName);

Use StreamReader to Read A Text File in .NET  (1725 Views)