How to Create a text file in ASP .NET ?
page 1 of 1
Published: 22 Oct 2003
Abstract
One of the frequent task in any web application is dealing with the text files. In classic ASP, we used the FileSystemObject to deal with files. In this article, we will see how to create text files in ASP .NET.
by Jesudas Chinnathampi (Das)
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 54600/ 633

How to Create a text file in ASP .NET ?

Written on: May, 13th 2002.
Introduction

One of the frequent task in any web application is dealing with the text files. In classic ASP, we used the FileSystemObject to deal with files. In this article, we will see how to create text files in ASP .NET.

Things that we will be learning in this article
  1. The namespace that is required to deal with Files
  2. The StreamWriter Object
  3. Creating a Text File
  4. How to capture errors which may occur while creating 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 Create a text File?

To start with, we need to create an instance of the object, StreamWriter. The instance will be the file pointer for us. Once we have a File Pointer, we need to invoke the method, CreateText method of the object, File. The method, CreateText 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 create a 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 StreamWriter

        Try
            fp = File.CreateText(Server.MapPath(".\Upload\") & "test.txt")
            fp.WriteLine(txtMyFile.Text)
            lblStatus.Text = "File Succesfully created!"
            fp.Close()
        Catch err As Exception
            lblStatus.Text = "File Creation failed. Reason is as follows

" & err.ToString()
        Finally

        End Try

    End Sub

How it works?

We are first creating an instance of StreamWriter, which is termed as fp (file pointer). Then, in the Try block, we invoke the CreateText method. To write the content, we use the method, WriteLine. Actually, we have just three lines of code which writes data to a text file. Once we have successfully written to the text file, we close the StreamWriter by invoking the Close method of StreamWriter.

Sample output of our scenario

Creating Text Files in ASP .NET - 15,605  bytes
Fig: Creating Text Files in ASP .NET

Test this Script

Download the code

Click here to download the ASPX page

Conclusion

Creating a text file is very easy. We can easily create a text file in just 3 lines of code. To read content from a text file, visit my article Read data from Text File

Links

Textbox Web Server Control
Reading data from a Text File

Send your comments to das@aspalliance.com       


Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 4 and 2 and type the answer here:

User Comments

Title: Pro Bono producer   
Name: Curt cagreer@att.net
Date: 4/18/2008 1:36:48 PM
Comment:
Article address's what I want but with my limited knowledge I am lost. Have a excel workbook that I create to sheets in these are maile & maild. I want to be able to take data from these sheets to a text file for mail merge use. Word mail merge searches entire excel sheet this takes to long. Have never done this before. Thanks
Title: Here is C# code of this article   
Name: isozworld (Ismail OZER)
Date: 3/12/2008 6:05:54 PM
Comment:
\
\
Title: It is realy clear.   
Name: isozworld (Ismail ÖZER)
Date: 3/12/2008 6:03:28 PM
Comment:
hi,
thank you very much for your clear and easy to understand work.
Title: how to create .aspx ?   
Name: KCJ
Date: 12/26/2007 7:18:33 AM
Comment:
Hai
how to create .aspx file from master pages... means dynamicaly i want to create content pages from ther master pages...
is it possible..
plz mail me "kcjagadeep at yahoo.com"
Title: hi   
Name: ray
Date: 12/16/2007 10:30:07 AM
Comment:
how about c#?
Title: Real Cool   
Name: Lennie
Date: 11/21/2007 1:54:35 PM
Comment:
This article is so practical and so easy to understand. Thank you very much for sharing your experiences and knowledges with us.
Title: :)   
Name: RED
Date: 11/11/2007 6:36:09 AM
Comment:
Very Helpfull
Tx a lot
Title: TY   
Name: PotHead
Date: 8/24/2007 1:58:26 AM
Comment:
Ty 4 this source...it works
Title: Hello   
Name: Dip
Date: 8/23/2007 4:42:25 AM
Comment:
It helped me a lot!
Title: Creating folder at client's machine using ASP.NET   
Name: RAHUL KATE
Date: 7/31/2007 6:20:24 AM
Comment:
Hi,
I want to create a Folder and an Excel Sheet on the Client's PC using ASP.NET WebForms. Any Idea is it possible using C#?

Any Help Greatly Appreciated.
Regards,
Rahul
Title: good article helps it lot   
Name: Dastagiri
Date: 5/21/2007 2:29:57 AM
Comment:
this is very helpfull and keep help people as you know that you will get more complements knowledge
Title: how to create text file on client side's C:\   
Name: b.s.srinu
Date: 1/11/2007 4:15:47 AM
Comment:
plz anyone know tell me how to create a client side file.

I want to create a text file on each client machines and access it for printing.

Printing I solved, now problem is of creation of files on client machine
Title: Great Article!   
Name: John Dunlap
Date: 11/19/2006 9:23:03 PM
Comment:
Great article - very easy to understand, and very practical. Good Job! :)
Title: How to Create a text file in ASP .NET ?   
Name: ashita
Date: 9/4/2006 3:00:21 AM
Comment:
It is realy a good article which has solved my problem.
Title: how to create a text file   
Name: Ramesh
Date: 8/14/2006 6:50:20 AM
Comment:
it's goood
Title: thankyou dear   
Name: nasir
Date: 7/11/2006 8:45:24 AM
Comment:
thankyou dear
Title: Permission   
Name: AF
Date: 6/8/2006 11:23:27 AM
Comment:
What about permissions ?
I have some problems when I create the text file (System.Security.Permissions.FileIOPermission)
Title: to create save multiple file on single clik   
Name: swati
Date: 6/3/2006 5:35:36 AM
Comment:
hi i want to download a single file and image files linked to that file how can i do it
Title: To create no. of files in a single folder   
Name: BKDas
Date: 5/29/2006 1:56:52 AM
Comment:
I want to create multiple files by selecting files through browse option in a single folder.Can u help me?
Title: How to open a notepad application using asp.net code   
Name: Arun Kumar.P
Date: 5/24/2006 12:12:43 PM
Comment:
This is an excellent example to create a text file. Could you explain how to open a notepad application file using asp.net.
Title: how to create text file on client side's C:\   
Name: sunil
Date: 3/25/2006 6:13:46 AM
Comment:
plz anyone know tell me how to create a client side file.

I want to create a text file on each client machines and access it for printing.

Printing I solved, now problem is of creation of files on client machine.
Title: Concept is good   
Name: Amit
Date: 2/22/2006 4:28:31 PM
Comment:
Good article send more articles
Title: How to Create a text file in ASP .NET ?   
Name: Meghana
Date: 10/21/2005 2:14:26 AM
Comment:
Good one!!
Thanks....
Title: How to Create a text file in ASP .NET ?   
Name: Azlan
Date: 10/21/2005 12:06:13 AM
Comment:
Thanks!!!!!
Title: how do you get that file to the client   
Name: ed
Date: 7/19/2005 2:35:05 PM
Comment:
how would you send that file to the client because right now it is on the server and no good to anyone?????
Title: creating folder   
Name: prabhakar dwivedi
Date: 5/31/2005 10:11:06 AM
Comment:
can you give me the idea for creating folder in asp.net
Title: FP.Close()   
Name: Uh-oh!
Date: 4/13/2005 2:06:03 PM
Comment:
You should put the fp.Close() in the finally branch so the file gets closed if there was an error.
Title: How to Create a text file in ASP .NET ?   
Name: Don Sanders
Date: 2/26/2005 5:46:21 PM
Comment:
This was an excellent article that solved my problem completely.






Ads Powered by Lake Quincy Media
Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 5/12/2008 1:10:57 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search