Curriculum
File Handling in C# is the process of creating, reading, writing, updating, and deleting files using C# programming. File Handling in C# allows applications to store data permanently, exchange information between systems, generate reports, manage logs, and process documents. Every .NET developer must understand File Handling in C# because it is widely used in Console Applications, ASP.NET Core Applications, MVC Projects, Web APIs, Desktop Applications, and Enterprise Software Solutions.
Modern applications frequently work with text files, configuration files, CSV files, log files, and documents. Understanding File Handling in C# helps developers build efficient and data-driven applications.
File Handling in C# refers to the process of performing operations on files stored in a computer’s file system.
Common file operations include:
These operations help applications store and retrieve information outside memory.
File Handling in C# helps developers:
Without File Handling in C#, application data would be lost when the program closes.
A file is a collection of data stored on a storage device.
Examples:
student.txt
report.pdf
employees.csv
data.json
A directory (folder) is used to organize files.
Example:
C:\Projects
Understanding both concepts is important for effective File Handling in C#.
C# provides file handling functionality through:
using System.IO;
The System.IO namespace contains classes for managing files, directories, and data streams.
Most File Handling in C# operations depend on this namespace.
The File class provides static methods for file operations.
Common methods include:
| Method | Purpose |
|---|---|
| Create() | Create file |
| ReadAllText() | Read file content |
| WriteAllText() | Write content |
| AppendAllText() | Add content |
| Delete() | Delete file |
| Copy() | Copy file |
| Move() | Move file |
The File class is one of the most frequently used classes in File Handling in C#.
Example:
using System.IO;
File.Create("student.txt");
This creates a new file named:
student.txt
If the file already exists, an exception may occur.
The WriteAllText() method writes content to a file.
Example:
using System.IO;
File.WriteAllText(
"student.txt",
"Welcome to C# File Handling"
);
File Content:
Welcome to C# File Handling
If the file exists, previous content is overwritten.
The ReadAllText() method reads all file content.
Example:
using System.IO;
string content =
File.ReadAllText("student.txt");
Console.WriteLine(content);
Output:
Welcome to C# File Handling
This method is useful for small files.
AppendAllText() adds content without removing existing data.
Example:
using System.IO;
File.AppendAllText(
"student.txt",
"\nLearning .NET Development"
);
Updated File:
Welcome to C# File Handling
Learning .NET Development
Appending is commonly used for logs and reports.
Before accessing a file, developers should verify its existence.
Example:
using System.IO;
if(File.Exists("student.txt"))
{
Console.WriteLine("File Found");
}
Output:
File Found
This prevents unnecessary exceptions.
Example:
using System.IO;
File.Delete("student.txt");
The file is permanently removed from the file system.
Developers should use this operation carefully.
Example:
using System.IO;
File.Copy(
"student.txt",
"backup.txt"
);
A duplicate file is created.
This is useful for backups and version management.
Example:
using System.IO;
File.Move(
"student.txt",
"Data/student.txt"
);
The file is moved to a different location.
Moving files helps organize application data.
The Directory class manages folders.
Example:
using System.IO;
Directory.CreateDirectory("Students");
Output:
Students Folder Created
Directories help organize large amounts of data.
Example:
using System.IO;
if(Directory.Exists("Students"))
{
Console.WriteLine("Directory Exists");
}
Output:
Directory Exists
This helps avoid runtime errors.
StreamWriter writes data to files efficiently.
Example:
using System.IO;
StreamWriter writer =
new StreamWriter("data.txt");
writer.WriteLine("Welcome");
writer.WriteLine("Learning C#");
writer.Close();
File Content:
Welcome
Learning C#
StreamWriter is useful for large-scale writing operations.
StreamReader reads data from files efficiently.
Example:
using System.IO;
StreamReader reader =
new StreamReader("data.txt");
string content =
reader.ReadToEnd();
Console.WriteLine(content);
reader.Close();
Output:
Welcome
Learning C#
StreamReader is preferred for large files.
The using statement automatically releases resources.
Example:
using(StreamWriter writer =
new StreamWriter("data.txt"))
{
writer.WriteLine("Hello");
}
Benefits:
Professional applications often use this approach.
Common exceptions include:
Occurs when a file does not exist.
Occurs when permission is denied.
Occurs when the directory path is invalid.
Occurs during general file input/output problems.
Exception Handling should always be combined with File Handling in C#.
Example:
try
{
string content =
File.ReadAllText("data.txt");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
This improves application reliability.
Store student records:
File.WriteAllText(
"students.txt",
studentData
);
Store transaction logs:
File.AppendAllText(
"transactions.txt",
transactionDetails
);
Generate invoices:
File.WriteAllText(
"invoice.txt",
invoiceData
);
Store patient records:
File.WriteAllText(
"patients.txt",
patientData
);
File Handling in C# is widely used in business applications.
File Handling in C# is heavily used in:
A strong understanding of File Handling in C# helps developers build reliable and professional applications.
File Handling in C# is the process of creating, reading, writing, updating, and deleting files.
The System.IO namespace is used for file and directory operations.
The File class provides static methods for performing common file operations.
StreamReader reads data from files, while StreamWriter writes data to files.
Exception Handling prevents application crashes caused by file access errors.
It enables applications to store, retrieve, process, and manage data permanently.
WhatsApp us