Home » CodeDigest


Read a Text File in C# and ASP.Net
Submitted By Satheesh Babu B
On 11/29/2008 10:34:28 PM

Read a Text File in C# and ASP.Net

.Netframework has lots of classes and methods for doing File manipulations. One of the frequently done task is to read the content of text file and display it in a ASP.Net page.

Assume we have a text file called filetext.txt copied to the project directory of our ASP.Net application. The following code snippet will help us to read the file content and assign it to a StringBuilder object using StreamReader class in System.IO namespace.


string root = Server.MapPath("~");
string Template = root +"\\filetext.txt";
StringBuilder line = new StringBuilder();
using (StreamReader rwOpenTemplate = new StreamReader(Template))
while (!rwOpenTemplate.EndOfStream)

In the above code, the Server.MapPath("~") will give us the root location 

You need to include System.IO and System.Text namespace for this code to work.


Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..