How to open a file in a Directory and how to modify the content in that file using C#.net..?


In this article I'm trying to explain how to open an existing file and how to modify the data in that file and how to update new data into that file. During this we learn how to work around with file opening and saving in particular driver location. This article will help you for beginners.

Opening a File:



In this article I'm trying to explain how to open an existing file and how to modify the data in that file and how to update new data into that file. During this we learn how to work around with file opening and saving in particular driver location. This article will help you for beginners.

Follow below steps to achieve your goal.

Step-1:


Open your project then Right click on solution explorer then Add-> NewItem then choose WebForm then give a name for that as "OpenFile.aspx" and then click on Add button, this will added the page into your project.

Step-2:


Then open a design view of that page then design page simply drag and drop 4 label controls, 2 textbox controls and 1 button control then design the view in below manner.

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

<!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="Label1" runat="server" Text="Enter Path and name of the File : "></asp:Label>
<asp:TextBox ID="txtFilePath" runat="server"></asp:TextBox>
<br />
<br />
Enter text to Replace data of file :
<asp:TextBox ID="txtWrtFile" runat="server" Height="67px" TextMode="MultiLine"
Width="422px"></asp:TextBox>
<br />
<br />
<br />
<asp:Button ID="btnfile" runat="server" onclick="btnfile_Click"
Text="Open file" />
<br />
<br />
<asp:Label ID="lblResult" runat="server"></asp:Label>
<br />
<br />
<asp:Label ID="lblStatus" runat="server"></asp:Label>

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



Step-3:


Once design view has been completed then view the design of that page that becomes look like below.

1

Step-4:


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

protected void btnfile_Click(object sender, EventArgs e)
{
System.IO.FileStream fs;
try
{
fs = System.IO.File.Open(txtFilePath.Text, System.IO.FileMode.Open);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
if (System.IO.File.Exists(txtFilePath.Text))
{
lblResult.Text = "File is opened";
sw.WriteLine(txtWrtFile.Text);
sw.Close();
lblStatus.Text = "File is saved at " + txtFilePath.Text + " and data is replaced with new data";
}
}
catch (Exception ex)
{
lblStatus.Text = ex.Message;
}
}


Step-5:


Now, the code ready to execute just click on F5 on keyboard then see the result set.

2

Conclusion:


I refer this article from ASP.net 4.5 Black Book. Hope this article will help you those who are looking for the same and those who are beginners to perform the same task.


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: