Overblog Follow this blog
Edit post Administration Create my blog
Balavardhan Reddy Narani

Read and Write Text Files in ASP.Net

March 7 2009 , Written by Balavardhan Published on #ASP.Net

This article is Creating Text Files in ASP .NET.
In this article, we will see how to read and Write text file in ASP .NET.

We require the namespace, System.IO to work with files. So, we should import this namespace in our ASPX page such as
using 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.

public void imgbut_create_click(object sender, ImageClickEventArgs e)
{
string msg_send = "Hi How are you Welcome to My World"
StreamWriter sw;
try
{
sw = File.CreateText("c://msgstoring.txt");
sw.WriteLine(msg_send);
sw.Close();
}
catch (Exception ex)
{
Response.Write("File Creation Failed Due to " + ex.Message.ToString());
}

}

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.

public void imgbut_create_click(object sender, ImageClickEventArgs e)
{
StreamReader sr;
try
{
sr = File.OpenText("c://msgstoring.txt");
lblmsg.Text = sr.ReadToEnd();
sr.Close();
}
catch (Exception ex)
{
Response.Write("File Reading Failed Due to " + ex.Message.ToString());
}

}

Share this post

Repost 0

Comment on this post

fiona 04/14/2011 10:10



Wholesale Gucci Handbags
Wholesale Chanel Handbags
Wholesale D&G Handbags
Wholesale Coach Wallets
Wholesale LV Wallets
Wholesale Gucci Wallets
Wholesale Chanel Wallets
Wholesale LV Sunglasses
Wholesale Gucci Sunglasses
Wholesale Chanel Sunglasses
Wholesale D&G Sunglasses
Wholesale nfl jerseys
wholesale jerseys
wholesale football jerseys
authentic jerseys
Wholesale nfl jersey
wholesale nhl jerseys
wholesale hockey jerseys
discount nhl jerseys
Wholesale nba jerseys
wholesale basketball jerseys
discount nba jerseys
Wholesale mlb jerseys
wholesale baseball jerseys
discount mlb jerseys
Wholesale NCAA Jerseys
Wholesale Custom Jersey
Nike Air Max
Air Max
Nike Air Max Shoes
Air Max Shoes
Nike Air Max 2010
Air Max 95
Air Max 90
Air Max LTD
Air Max TN
Air Max Skyline
Air Max Classic



fiona 04/14/2011 10:08



Wholesale Gucci Handbags
Wholesale Chanel Handbags
Wholesale D&G Handbags
Wholesale Coach Wallets
Wholesale LV Wallets
Wholesale Gucci Wallets
Wholesale Chanel Wallets
Wholesale LV Sunglasses
Wholesale Gucci Sunglasses
Wholesale Chanel Sunglasses
Wholesale D&G Sunglasses
Wholesale nfl jerseys
wholesale jerseys
wholesale football jerseys
authentic jerseys
Wholesale nfl jersey
wholesale nhl jerseys
wholesale hockey jerseys
discount nhl jerseys
Wholesale nba jerseys
wholesale basketball jerseys
discount nba jerseys
Wholesale mlb jerseys
wholesale baseball jerseys
discount mlb jerseys
Wholesale NCAA Jerseys
Wholesale Custom Jersey
Nike Air Max
Air Max
Nike Air Max Shoes
Air Max Shoes
Nike Air Max 2010
Air Max 95
Air Max 90
Air Max LTD
Air Max TN
Air Max Skyline
Air Max Classic



Nike Shox Shoes 08/18/2010 05:57



It may be accept by individuals so simply. I feel lots of individuals will agree with you. You will be appropriate that we could to not rely on other people. Who we could to count on is ourself.