Description:-
Here in this example we will Explain
that how to Read and Write Text File in Asp.Net using C#.Net.
Reading and Writing text file
content in asp.net it’s very simple but we need to 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.
This is very useful Example because
Read and Write file in application is very Required when we working with upload
file database and again read or modify this file in database.
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:\kirit\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
Default.aspx:-
<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>
Default.aspx.cs:-
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();
}
}
}
0 comments:
Post a Comment