Followers

Thursday, 10 January 2013

how to read or write text file using asp.net



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


No comments:

Post a Comment