using System.IO; |
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> |
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(); } } } |
|
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 Email
|
|||
|
|
Subscribe by RSS
Subscribe by Email
11 comments :
ffffffffff
nice yaaar
very nice article
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
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
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
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
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)
sorry ...
string path = "~Docs"+fileupload1.PostedFile.FileName;
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