Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies


Introdcution

Here I will explain how to read or write text file using asp.net

Description

Reading and Writing text file content in asp.net it’s very simple once if we use name space called 

using System.IO;


The System.IO namespace contains types that allow reading and writing to files and data streams and types that provide basic file and directory support.

Check this link for clear information on this namespace Click Here

After that in code we will use Class File to get properties like

File.WriteAllLines: Creates a new file, writes one or more strings to the file, and then closes the file.

File.ReadAllLines: Opens a text file, reads all lines of the file into a string array, and then closes the file.

File.AppendAllText: Appends lines to a file, and then closes the file.

Here I am creating file in this path D:\Suresh\MyTest.txt first I will create text file in this path after that I will read the text from this text file and I will display that text in textbox.

Design your aspx page like this

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td align="right">
Enter path to store file:
</td>
<td>
<asp:TextBox ID="txtpath" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnCreate" runat="server" onclick="btnCreate_Click" Text="Create" />
<asp:Label ID="lbltxt" runat="server" ForeColor="Red"></asp:Label>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:FileUpload ID="fileupload1" runat="server" />
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnRead" runat="server" onclick="btnRead_Click" Text="Read" />
</td>
</tr>
<tr>
<td valign="top">
Message of Created Text file :
</td>
<td>
<asp:TextBox ID="textBoxContents" runat="server" tabIndex="0" height="100px" textMode="MultiLine" width="250px"></asp:TextBox>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

After that write the following namespace and code in code behind

using System.Text;


protected void btnCreate_Click(object sender, EventArgs e)
{
string path = txtpath.Text;//Give path in this format D:\Suresh\MyTest.txt

// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
string[] createText = { "Hello", "Welcome to aspdotnet-suresh", "Reading and writing text file" };
File.WriteAllLines(path, createText);
}
// This text is always added, making the file longer over time
// if it is not deleted.
string appendText = "This is extra text" + Environment.NewLine;
File.AppendAllText(path, appendText);
lbltxt.Text = "File Created Successfully";
}

protected void btnRead_Click(object sender, EventArgs e)
{
string path = fileupload1.PostedFile.FileName;
if(!string.IsNullOrEmpty(path))
{
string[] readText = File.ReadAllLines(path);
StringBuilder strbuild = new StringBuilder();
foreach (string s in readText)
{
strbuild.Append(s);
strbuild.AppendLine();
}
textBoxContents.Text = strbuild.ToString();
}
}
}

By using above code we can create word documents also during the time creating we need to give name like this D:\Suresh\MyTest.doc instead of .txt we need to give .doc

Demo




If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

22 comments :

Anonymous said...

ffffffffff

Anonymous said...

nice yaaar

Manoj said...

very nice article

Imtiaz said...

is there any way to read word document. Like in job portals user has to fill too many fields Name, qualifications, position applied for etc is there a way that asp.net read all the related value from user cv word document and enter it in the sql data base relevant to save the time?

feedback will be hihgly appreciated

Raj Trivedi said...

Hello Sir nice article but can you tell me how can i read specific word files...
I have uploaded .doc files in my application folder and used a Link button to download the file now what i want is how i can read the file in asp.net by clicking on the link of that download button

Anonymous said...

Hi Suresh,

The below code is working only in IE.How to do the same using Firefox

string path = fileupload1.PostedFile.FileName;

I tried to save the file on server and written the following code

string path = "~/Docs/" + fileupload1.FileName;
fileupload1.SaveAs(MapPath(path));
if (File.Exists(Server.MapPath(path)))
{
string[] readText = File.ReadAllLines(path);
StringBuilder strbuild = new StringBuilder();
foreach (string s in readText)
{
strbuild.Append(s);
strbuild.AppendLine();
}
textBoxContents.Text = strbuild.ToString();
}

but still not working..Can you please help me.

Thanks.

Prathap

wat said...

vinoth:
im doing project in asp.net in cloud.
can u help me to create asp.net encrypt and decrypt the text file while uploading .
this is one module of my project.
give the tips.give ur sample project.
email id:watmurgan@gmail.com

himanshu said...
This comment has been removed by the author.
Anonymous said...

string path = "~/Docs/" + fileupload1.FileName;
change in above line....

string path = "~Docs" + fileupload1.FileName;

(Docs is a folder name....it doesn't matter at which location it exist but the file must be docs named folder)

Anonymous said...

sorry ...
string path = "~Docs"+fileupload1.PostedFile.FileName;

Anonymous said...

if u do not want to specify folder name..... just remove the folder name....

string path = "~"+FileUpload1.PostedFile.FileName;

by this u can read file from any location....
ashish

powerful peraiah said...

sir i want to read the document type .doc or .docx . I want to display the content in that document file in to a textbox having the property as multiline text mode. For example: i uploaded a resume and i want to display that resume data on to a text box what is the code in c#.I taken the resume in database table of datatype as varbinary(max).

venkatachalam A said...

when sent email its take time too long (20 seconds) so how to solve that performance issue

Anto Reegan said...

Could we create .htm file like this..?

Anonymous said...

sir i want to read the document type .doc or .docx . I want to display the content in that document file in to a textbox having the property as multiline text mode. For example: i uploaded a resume and i want to display that resume data on to a text box what is the code in c#.I have kept the uploaded resume in a folder added in my project. I m getting some Encoded data when trying to read it by streamreader or by File.ReadAllLines(path);

Siva Krishna said...

ReadAllLines(path). won't work for .doc or .docx. is there any other way ?

Doug said...

Thanks Suresh,

Your post helped with some code I am working on.

Anonymous said...

Suresh,
The same source code of urs is found out in below mentioned link...

http://aspsolutionkirit.blogspot.in/2013/09/how-to-read-or-write-text-file-using.html

MANI K said...

Thanks for your code.

Ahsan Iqbal said...

very nice your so sweet ..
but there is error in this line:
StringBuilder strbuild = new StringBuilder();
plz plz tell me about "StringBuilder"

Gohil Jaydip said...

Hey Sureshbhai , please help me bro...
i develope this above application by your code but i found error like
Could not find file 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\1.txt'.
please yaar please solve my error.....

Anonymous said...

Hello !! I need To Find Out String in PDF and Also Highlight that Text

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.