1. Introduction
In any real-world ASP.NET project, file handling is essential—whether you’re saving logs, generating reports, or managing uploaded files.
C# makes this process simple using the System.IO namespace, which provides classes such as:
File– For simple file operations.StreamReader/StreamWriter– For reading and writing text.FileInfo,Directory,Path– For managing files and folders.
2. Common Real-Time Use Cases in Web Applications
| Scenario | Purpose |
|---|---|
| Save Logs | Save error or user activity logs in text files. |
| Upload Files | Upload user files and save them on the server. |
| Read Reports | Read data from stored files. |
| Export Data | Create text-based or CSV reports. |
| Manage Files | Delete or rename temporary files. |
3. Real-Time Example: File Handling in ASP.NET Web Forms
Let’s create a simple WebForm that performs three operations:
Write to a file – save user input
Read file content – show it in a text box
Delete the file – remove it when not needed
Step 1: Create a WebForm – FileHandler.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileHandler.aspx.cs" Inherits="FileHandler" %>
<!DOCTYPE html>
<html>
<head>
<title>File Handling in ASP.NET Web Forms</title>
</head>
<body>
<form id="form1" runat="server">
<div style="width:600px; margin:auto; text-align:center; font-family:Arial;">
<h2>📂 File Handling Example (Read / Write / Delete)</h2>
<asp:Label ID="lblInfo" runat="server" Text="Enter some text:" /><br />
<asp:TextBox ID="txtContent" runat="server" TextMode="MultiLine" Rows="5" Width="400px"></asp:TextBox><br /><br />
<asp:Button ID="btnWrite" runat="server" Text="Write to File" OnClick="btnWrite_Click" />
<asp:Button ID="btnRead" runat="server" Text="Read File" OnClick="btnRead_Click" />
<asp:Button ID="btnDelete" runat="server" Text="Delete File" OnClick="btnDelete_Click" /><br /><br />
<asp:Label ID="lblMessage" runat="server" Font-Bold="True" ForeColor="Green"></asp:Label>
</div>
</form>
</body>
</html>
Step 2: Code Behind – FileHandler.aspx.cs
using System;
using System.IO;
public partial class FileHandler : System.Web.UI.Page
{
string filePath = HttpContext.Current.Server.MapPath("~/App_Data/UserData.txt");
protected void Page_Load(object sender, EventArgs e)
{
// Ensure directory exists
string directoryPath = Path.GetDirectoryName(filePath);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
}
// WRITE TO FILE
protected void btnWrite_Click(object sender, EventArgs e)
{
try
{
string content = txtContent.Text;
// Write text to file
File.WriteAllText(filePath, content);
lblMessage.Text = " Data written to file successfully!";
}
catch (Exception ex)
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = " Error: " + ex.Message;
}
}
// READ FILE CONTENT
protected void btnRead_Click(object sender, EventArgs e)
{
try
{
if (File.Exists(filePath))
{
string data = File.ReadAllText(filePath);
txtContent.Text = data;
lblMessage.Text = " File content loaded successfully!";
}
else
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = " File not found!";
}
}
catch (Exception ex)
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = " Error: " + ex.Message;
}
}
// DELETE FILE
protected void btnDelete_Click(object sender, EventArgs e)
{
try
{
if (File.Exists(filePath))
{
File.Delete(filePath);
txtContent.Text = "";
lblMessage.Text = " File deleted successfully!";
}
else
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = " File does not exist!";
}
}
catch (Exception ex)
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = " Error: " + ex.Message;
}
}
}

Join the conversation! Your thoughts help the community grow.