How to Reading and Appending Text to a file in ASP.net.?


In this article I'm trying to explain how to read file data and how to append text to that file using C#.net. Here, we just give a file name based on that file name it will fetch total server path and get the data in that file and displayed that into our application. This article will help you for beginners those who are perform the same task.

Reading and Appending Text to a file:



In this article I'm trying to explain how to read file data and how to append text to that file using C#.net. This article will help you for beginners those who are perform the same task.

Follow below steps to achieve your goal.

Step-1:



Create a project and then right click on solution explorer then Add->New Item to that project and give a name for that as "AppendText.aspx".

Step-2:



Once Web Form has been created under your project then designs the form by dragging and dropping 2 label controls and 2 Textbox controls and 2 Button controls into that design view.

Step-3:



Once design the form then the design view is looks becomes like below.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AppendText.aspx.cs" Inherits="ASPnet_AppendText" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Label ID="lblPath" runat="server"
Text="Enter Path and name of the file : "></asp:Label>

<asp:TextBox ID="txtPath" runat="server"></asp:TextBox>
<br />
<br />
<asp:TextBox ID="txtText" runat="server" Height="110px" TextMode="MultiLine"
Width="457px"></asp:TextBox>
<br />
<br />
<br />
<asp:Button ID="btnRead" runat="server" onclick="btnRead_Click" Text="Read" />

<asp:Button ID="btnAppend" runat="server" onclick="btnAppend_Click"
Text="Append" />
<br />
<br />
<asp:Label ID="lblResult" runat="server"></asp:Label>

</div>
</form>
</body>
</html>


Step-4:



Once source code design has been completed then the design view is looks becomes like below.

1

Step-5:



Once design has been completed then double click on Read button and wrote below lines of code under Read button click event.

protected void btnRead_Click(object sender, EventArgs e)
{
try
{
//get server file path
string filename = Server.MapPath(txtPath.Text);
System.IO.StreamReader reader;
//read the content inside of that file
reader = System.IO.File.OpenText(filename);
string str = reader.ReadToEnd();
//append read file text to control
txtText.Text = str;
//close the reader
reader.Close();
lblResult.Text = "File Read successfully";
}
catch (Exception ex)
{
lblResult.Text = ex.Message;
}
}


Step-6:



Now execute the page and then see the file has been read successfully or not.

2

Step-7:



Once Read the file part has been completed then close the execution browser and then double click on the Append button and then wrote the below lines of code under Append button click event.

protected void btnAppend_Click(object sender, EventArgs e)
{
try
{
//get server file path
string filename = Server.MapPath(txtPath.Text);
System.IO.StreamWriter writer;
//Append text to that file
writer = System.IO.File.AppendText(filename);
writer.WriteLine(txtText.Text);
writer.Close();
lblResult.Text = "Text Append successfully";
}
catch (Exception ex)
{
lblResult.Text = ex.Message;
}
}

Step-8:



Once code part has been completed then execute the code by pressing F5 on keyboard and then see the result.

3

Conclusion:



I hope this article will help you those who are looking for the same and those who are beginners to perform the same task.


Article by naveensanagasetti
I hope you enjoyed to read my article, If you have any queries out of this then please post your comments.

Follow naveensanagasetti or read 139 articles authored by naveensanagasetti

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: