How to Read a text file in ASP .NET ?
page 1 of 1
Published: 17 Oct 2003
Unedited - Community Contributed
This article is a continuation of the article, Creating Text Files in ASP .NET. In this article, we will see how to read a text file in ASP .NET.
by Jesudas Chinnathampi (Das)
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 44573/ 31

How to Read a text file in ASP .NET ?

Written on: May, 13th 2002.

This article is a continuation of the article, Creating Text Files in ASP .NET. In this article, we will see read the text file which we created earlier.

Things that we will be learning in this article
  1. The namespace that is required to deal with Files
  2. The StreamReader Object
  3. Reading a Text File
  4. How to capture errors which may occur while reading a text file?

The Namespace that is required to deal with Files

We require the namespace, System.IO to work with files. So, we should import this namespace in our ASPX page such as

<%@ Import Namespace="System.IO" %>

How to Read a text File?

To start with, we need to create an instance of the object, StreamReader. The instance will be the file pointer for us. Once we have a File Pointer, we need to invoke the method, OpenText method of the object, File. The method, OpenText takes a string as an argument. The string is nothing but the path of file that is going to get created. Now, let us see an example. Let us assume, we have a textbox with textmode set to MultiLine and a button. On the click event of the button, we need to read the text file. The code within the Click event is shown below.

Code in the OnClick event of button.
    Sub WriteToFile(sender As Object, e As EventArgs)

        Dim fp As StreamReader

            fp = File.OpenText(Server.MapPath(".\Upload\") & "test.txt")
            txtMyFile.Text = fp.ReadToEnd()
            lblStatus.Text = "File Succesfully Read!"
        Catch err As Exception
            lblStatus.Text = "File Read Failed. Reason is as follows

" & err.ToString()

        End Try

    End Sub

How it works?

We are first creating an instance of StreamReader, which is termed as fp (file pointer). Then, in the Try block, we invoke the OpenText method. To read the content, we use the method, ReadToEnd. Actually, we have just three lines of code which reads content from a text file. Once we have read the content from the text file, we close the StreamReader by invoking the Close method of StreamReader.

Test this Script

Download the code

Click here to download the ASPX page


Reading a text file is just the same way as creating the textfile. We can also use the method Read or ReadLine to read data from a text file.


Creating a Text File
Textbox Web Server Control

Send your comments to        

User Comments

Title: ty   
Name: ytr
Date: 2012-11-06 1:45:52 AM
Title: .net Programmer   
Name: T
Date: 2012-06-27 1:06:56 PM
Great information. How can I access to text file that is hosted on another server?
Title: Gracias   
Name: Rafa
Date: 2011-08-09 3:11:57 PM
Gracias ...
Title: Thanks   
Name: junric
Date: 2011-02-14 4:48:06 AM
Title: Thanks   
Name: AtulvermA
Date: 2011-02-07 12:02:29 AM
Thanks buddy....
it works and could you provide here some additional string operation
Title: mono   
Name: wrr
Date: 2010-12-21 12:17:47 PM
Title: Nice Blog Feature   
Name: k
Date: 2010-11-05 2:40:44 PM
How did you create this blog?
Title: txtFile   
Name: Aftab hassan
Date: 2010-10-07 9:36:26 AM
really good code,
but pls pls pls pls can u post the C# version.

Title: txtfile   
Name: Rohit
Date: 2010-09-11 12:33:44 PM
give me the code in c# to read a file from an directory.....
Title: textfile   
Name: aamirshah
Date: 2010-07-30 3:41:23 AM
dear i like that cde its good
Title: asp   
Name: gaurav nigam
Date: 2010-06-10 11:31:22 AM
dear i like that cde its good
Title: Asp   
Name: tintin
Date: 2010-04-27 4:35:52 AM
Hey it was a good article.. Can u put about asp file object
Title: Reading data from file   
Name: Suresh
Date: 2009-12-22 4:01:07 AM
This code is very useful and working 100%.

Title: how to read the text file   
Name: srinath
Date: 2009-10-07 1:45:58 AM
excellent information about textfile
Title: how to read the text file   
Name: Ramesh
Date: 2009-09-08 2:55:53 AM
very usefull for this code...
Title: how to create a text file using in C#   
Name: prabakar
Date: 2009-06-18 12:58:33 AM
I like to know how to create a text or config file in c# using and add data to it////

Title: How Can Read text file   
Name: shiav
Date: 2009-04-01 3:08:46 AM
Plz tell me how can read the text file.
Title: No Use   
Name: sreenivas
Date: 2009-02-11 3:42:54 AM
Can u plz send me this code in
Title: how to display text file content in aspxpage   
Name: Prasanth.Moganti
Date: 2009-01-05 1:30:54 AM
I want to display the text file content in aspx page can any one help
Title: how to read a file in client system   
Name: Lizy
Date: 2008-12-10 4:56:53 AM
Please let me know how to read a file in client system
Title: reading text file problem   
Name: Hamayun
Date: 2008-10-28 1:31:25 AM
When I read text from txt file All £ sign are converted to ? marks
I don't know why
Title: Miss   
Name: Grace
Date: 2008-07-16 1:13:22 PM
I want to read web logs files in the folder according to the specified dates (start date & end date).
The input is "start date (1-12-2007) and end date (10-12-2007)" and click "upload" then,

(ex011207.log to ex101207.log) is uploaded .
Title: Great Job   
Name: Da Wen Yu
Date: 2008-03-28 8:38:08 PM
I was looking for a solution to this problem for several days. Your code solves my problems within minutes.
Title: How to Read a text file in ASP .NET   
Name: Raj
Date: 2007-10-29 1:39:17 AM
Hey, this article is cool dude. It has come very useful for me. I really appreciate the way you were expalining the code. Applauds/

Title: how to Read uploaded file   
Name: sumit
Date: 2007-10-12 3:38:12 AM
while uploading a file i have to read the text of the file and then store the text to destination in a new file...
please help me with this..
Title: reading text file   
Name: hirdesh sharad
Date: 2007-08-13 1:55:24 AM
this is very good code
very useful
Title: Hm,Useful   
Name: Devarshi
Date: 2007-07-25 2:54:28 PM
Hi dude,
i go through ur artical,it is small but it is excellent and very useful.I like it.
Title: how can i find a last visitor of web site is   
Name: savita singh
Date: 2007-06-27 8:38:01 AM
plz give me the asnwer of this question .
wat is the diff b/w ojb based or ojb oriented language

with regards
savita singh
Title: How to Read a .doc File? through ASP   
Name: Ravi Ranjan
Date: 2007-06-26 8:18:55 AM
By help of this code i read form .txt file but this code not work for .doc file (ASP). So plz any one have solution of this problem then Mail to me
Title: Thanks   
Name: Suvir
Date: 2007-05-06 9:50:41 PM
Thanks its working good.
Title: Thank You   
Name: Abhi
Date: 2007-02-13 7:53:05 AM
Thank you very much for this usefull info..
Title: very helful   
Name: Ramesh
Date: 2006-11-15 8:03:12 AM
This file works,i am not able to read the .doc files
i got idia ,can u tell me how to read file from the system(Local machine)
Title: How to Read a text file in ASP .NET using c#   
Name: ssyadav
Date: 2006-07-12 5:10:38 PM
hello sir plz send this code .How to Read a text file in ASP .NET using c#

Title: Reading And Writing In The File   
Name: Nikeeta Vanjara
Date: 2006-06-08 5:21:22 AM
It Is Good To Know Reading And Writing In The File
Title: File   
Name: Reading And Writing From File
Date: 2006-06-08 5:19:27 AM
It Is Good
Title: good work done it helps me a lot   
Name: faheem ahmad
Date: 2006-02-26 11:43:17 PM
i got the basic idea.
it solves my problem.
keep it up.
Title: Wonderfull reading the file in Textbox   
Name: Dharmendra Jain
Date: 2005-10-25 7:33:55 AM
U hav read data from text file to text box. But one problem is there How can we take printout of the textbox only in which Text File data is stored.
If Any body know that then plz reply as soon as possible
Title: Great   
Name: Sree
Date: 2005-06-02 10:28:43 AM
Thank You very much for the codes (both creation and reading)
Title: very helpful   
Name: sunil pundir
Date: 2005-05-04 5:19:54 AM
this article is very useful to get some idea about the filehandling in ASP.NET
Title: wonderful   
Name: maily Ochoa
Date: 2005-01-20 9:30:07 AM
this file if it works, but the one of file creation never I work myself has a problem that does not give access him to the folder where it is due to create, this denied.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-05-24 9:56:08 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search