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.
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.
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">
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Enter Path and name of the File : "></asp:Label>
<asp:TextBox ID="txtFilePath" runat="server"></asp:TextBox>
Enter text to Replace data of file :
<asp:TextBox ID="txtWrtFile" runat="server" Height="67px" TextMode="MultiLine"
<asp:Button ID="btnfile" runat="server" onclick="btnfile_Click"
Text="Open file" />
<asp:Label ID="lblResult" runat="server"></asp:Label>
<asp:Label ID="lblStatus" runat="server"></asp:Label>
Once design view has been completed then view the design of that page that becomes look like below.
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)
fs = System.IO.File.Open(txtFilePath.Text, System.IO.FileMode.Open);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
lblResult.Text = "File is opened";
lblStatus.Text = "File is saved at " + txtFilePath.Text + " and data is replaced with new data";
catch (Exception ex)
lblStatus.Text = ex.Message;
Now, the code ready to execute just click on F5 on keyboard then see the result set.
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.
No responses found. Be the first to comment...