How to Read a text file in ASP .NET ?
page 1 of 1
Published: 17 Oct 2003
Unedited - Community Contributed
Abstract
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)
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 181533/ 179

How to Read a text file in ASP .NET ?

Written on: May, 13th 2002.
Introduction

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

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

" & err.ToString()
        Finally

        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

Conclusion

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.

Links

Creating a Text File
Textbox Web Server Control

Send your comments to das@aspalliance.com        



User Comments

Title: ty   
Name: ytr
Date: 2012-11-06 1:45:52 AM
Comment:
ytrytry
Title: .net Programmer   
Name: T
Date: 2012-06-27 1:06:56 PM
Comment:
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
Comment:
Gracias ...
Title: Thanks   
Name: junric
Date: 2011-02-14 4:48:06 AM
Comment:
Thanks
Title: Thanks   
Name: AtulvermA
Date: 2011-02-07 12:02:29 AM
Comment:
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
Comment:
good
Title: Nice Blog Feature   
Name: k
Date: 2010-11-05 2:40:44 PM
Comment:
How did you create this blog?
Title: txtFile   
Name: Aftab hassan
Date: 2010-10-07 9:36:26 AM
Comment:
really good code,
but pls pls pls pls can u post the C# version.

Thanks,
Tubby
Title: txtfile   
Name: Rohit
Date: 2010-09-11 12:33:44 PM
Comment:
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
Comment:
dear i like that cde its good
Title: asp   
Name: gaurav nigam
Date: 2010-06-10 11:31:22 AM
Comment:
dear i like that cde its good
Title: Asp   
Name: tintin
Date: 2010-04-27 4:35:52 AM
Comment:
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
Comment:
This code is very useful and working 100%.

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

thanks
Title: How Can Read text file   
Name: shiav
Date: 2009-04-01 3:08:46 AM
Comment:
Hello,
Plz tell me how can read the text file.
Title: No Use   
Name: sreenivas
Date: 2009-02-11 3:42:54 AM
Comment:
Can u plz send me this code in asp.net
Title: how to display text file content in aspxpage   
Name: Prasanth.Moganti
Date: 2009-01-05 1:30:54 AM
Comment:
I want to display the text file content in aspx page can any one help
Title: how to read a file in client system asp.net   
Name: Lizy
Date: 2008-12-10 4:56:53 AM
Comment:
Please let me know how to read a file in client system asp.net.
Title: reading text file problem   
Name: Hamayun
Date: 2008-10-28 1:31:25 AM
Comment:
Hello
When I read text from txt file All £ sign are converted to ? marks
I don't know why
Thanks
Title: Miss   
Name: Grace
Date: 2008-07-16 1:13:22 PM
Comment:
Hello,
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
Comment:
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
Comment:
Hey, this article is cool dude. It has come very useful for me. I really appreciate the way you were expalining the code. Applauds/

Raj/
Title: how to Read uploaded file   
Name: sumit
Date: 2007-10-12 3:38:12 AM
Comment:
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
Comment:
this is very good code
very useful
Title: Hm,Useful   
Name: Devarshi
Date: 2007-07-25 2:54:28 PM
Comment:
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 asp.net   
Name: savita singh
Date: 2007-06-27 8:38:01 AM
Comment:
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
Comment:
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
raviranjan249@gmail.com
Title: Thanks   
Name: Suvir
Date: 2007-05-06 9:50:41 PM
Comment:
Thanks its working good.
Title: Thank You   
Name: Abhi
Date: 2007-02-13 7:53:05 AM
Comment:
Thank you very much for this usefull info..
Title: very helful   
Name: Ramesh
Date: 2006-11-15 8:03:12 AM
Comment:
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
Comment:
hello sir plz send this code .How to Read a text file in ASP .NET using c#



thankx
Title: Reading And Writing In The File   
Name: Nikeeta Vanjara
Date: 2006-06-08 5:21:22 AM
Comment:
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
Comment:
It Is Good
Title: good work done it helps me a lot   
Name: faheem ahmad
Date: 2006-02-26 11:43:17 PM
Comment:
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
Comment:
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
Comment:
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
Comment:
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
Comment:
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-2017 ASPAlliance.com  |  Page Processed at 2017-03-28 5:40:02 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search