Read and write to file using ASP.NET (C#)
Reading and writing to the file content using ASP.NET 2.0 and C# 2.0 is actually very simple.
First, you will need to import the System.IO namespace.
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. Using the class of File ,and DirectoryInfo at here.
Use the DirectoryInfo class for typical operations such as copying, moving, renaming, creating, and deleting directories.
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
Use the File class for typical operations such as creating, opening, deleting, appending, copying, moving, and renaming to files.
Use the ReadAllText method to opens a text file, reads all lines of the file, and then closes the file.
Use the WriteAllText method Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.
We use the buttonDisplay_Click event to display the file content and use the buttonSave_Click even to save as a file.
The code as follows.
protected void buttonDisplay_Click(object sender, EventArgs e) {
string FilePath; FilePath = txtBoxInput.Text; if (File.Exists(FilePath)) {
textBoxContents.Text = File.ReadAllText(FilePath); } else {
Response.Write("<script language='javascript'>window.alert('File not found');</script>"); } } protected void buttonSave_Click(object sender, EventArgs e) {
DirectoryInfo Folder = new DirectoryInfo(textboxPath.Text); if (Folder.Exists) {
if (textboxName.Text != string.Empty) {
string FilePathSave = Folder.ToString() + textboxName.Text;
File.WriteAllText(FilePathSave, textBoxContents.Text); } else {
Response.Write("<script language='javascript'>window.alert('Please enter file name');</script>"); } } else { Response.Write("<script language='javascript'>window.alert('Folder not found');</script>"); } } |
The front ReadWriteTextCSharp.aspx page looks something like this:
We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.
<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<tr>
<td bgcolor="#eeeeee" class="header1"> <fieldset> <legend>ReadWriteTextCsharp</legend> <div> <asp:Label ID="Label1" runat="server" Text="Enter path of file to be examined and click Display"></asp:Label><br /> <asp:TextBox ID="txtBoxInput" runat="server" Width="451px"></asp:TextBox> <asp:Button ID="buttonDisplay" runat="server" Text="Display" OnClick="buttonDisplay_Click" /><br /> <fieldset> <legend>Content of file</legend> <asp:Label ID="Label2" runat="server" Text="You can edit the content and save as a file."></asp:Label><br /> <table>
<tr>
<td> <asp:TextBox ID="textBoxContents" runat="server" tabIndex="0" height="200px" textMode="MultiLine" width="450px"></asp:TextBox> </td> </tr> </table> <fieldset> <legend>Save as</legend>Path: <asp:TextBox ID="textboxPath" runat="server" Width="237px"></asp:TextBox>File name: <asp:TextBox ID="textboxName" runat="server" Width="93px"></asp:TextBox> <asp:Button ID="buttonSave" runat="server" Text="Save As" Width="66px" OnClick="buttonSave_Click" /></fieldset> </fieldset> </div> </fieldset> </td> </tr> </table>
|
The flow for the code behind page is as follows.
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO;
public partial class ReadWriteTextCSharp : System.Web.UI.Page {
protected void buttonDisplay_Click(object sender, EventArgs e) {
string FilePath; FilePath = txtBoxInput.Text; if (File.Exists(FilePath)) {
textBoxContents.Text = File.ReadAllText(FilePath); } else {
Response.Write("<script language='javascript'>window.alert('File not found');</script>"); } } protected void buttonSave_Click(object sender, EventArgs e) {
DirectoryInfo Folder = new DirectoryInfo(textboxPath.Text); if (Folder.Exists) {
if (textboxName.Text != string.Empty) {
string FilePathSave = Folder.ToString() + textboxName.Text;
File.WriteAllText(FilePathSave, textBoxContents.Text); } else {
Response.Write("<script language='javascript'>window.alert('Please enter file name');</script>"); } } else { Response.Write("<script language='javascript'>window.alert('Folder not found');</script>"); } } } |
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
Looking for the VB.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!