Curriculum
File Handling in C# is an essential skill for every .NET developer. File Handling in C# allows applications to create, read, write, update, copy, move, and delete files and directories. Whether you are building an ASP.NET Core Application, Desktop Application, Web API, Hospital Management System, Banking Application, ERP Software, CRM System, or Enterprise Software Solution, File Handling in C# is frequently used for storing logs, reports, configuration files, user data, backups, and document management.
Understanding File Handling in C# is important because most real-world applications interact with files stored on local machines, servers, cloud storage, and network drives.
File Handling in C# refers to the process of working with files and directories using the .NET Framework’s System.IO namespace.
The System.IO namespace provides classes for:
Namespace:
using System.IO;
Most file-related operations are performed through this namespace.
File Handling in C# helps developers:
File management is an important part of enterprise software development.
A file contains data.
Examples:
StudentReport.txt
Employees.csv
Config.json
Invoice.pdf
A directory (folder) organizes files.
Examples:
Students
Reports
Invoices
Documents
Applications often work with both files and directories.
The File class provides static methods for file operations.
Common methods:
Create
WriteAllText
ReadAllText
Copy
Move
Delete
Exists
These methods simplify file management.
Example:
File.Create(
@"C:\Files\Test.txt");
Output:
File Created
A new file is created at the specified location.
Example:
bool exists =
File.Exists(
@"C:\Files\Test.txt");
Console.WriteLine(
exists);
Output:
True
This helps prevent runtime errors.
Example:
File.WriteAllText(
@"C:\Files\Test.txt",
"Welcome to C# File Handling");
Output:
Content Written
The file content is replaced with new data.
Example:
string content =
File.ReadAllText(
@"C:\Files\Test.txt");
Console.WriteLine(
content);
Output:
Welcome to C# File Handling
This is one of the most commonly used file operations.
Example:
File.AppendAllText(
@"C:\Files\Test.txt",
"\nLearning File Handling");
Output:
Data Added
Existing content remains unchanged.
Example:
File.Copy(
@"C:\Files\Test.txt",
@"C:\Backup\Test.txt");
Output:
File Copied
This is useful for backup operations.
Example:
File.Move(
@"C:\Files\Test.txt",
@"C:\Archive\Test.txt");
Output:
File Moved
The file is transferred to a new location.
Example:
File.Delete(
@"C:\Files\Test.txt");
Output:
File Deleted
Always verify file existence before deletion.
The Directory class helps manage folders.
Common methods:
CreateDirectory
Delete
Exists
Move
GetFiles
GetDirectories
Directory management is frequently required in enterprise applications.
Example:
Directory.CreateDirectory(
@"C:\Reports");
Output:
Folder Created
The folder is created if it does not already exist.
Example:
bool exists =
Directory.Exists(
@"C:\Reports");
Output:
True
This helps avoid exceptions.
Example:
string[] files =
Directory.GetFiles(
@"C:\Reports");
Output:
List of Files
Useful for report processing systems.
A stream represents a sequence of bytes flowing between a source and destination.
Examples:
File Streams
Network Streams
Memory Streams
Streams provide greater control over file operations.
StreamWriter writes data to files.
Example:
StreamWriter writer =
new StreamWriter(
@"C:\Files\Data.txt");
writer.WriteLine(
"Welcome to C#");
writer.Close();
Output:
Data Saved
StreamWriter is widely used in logging systems.
Example:
using(StreamWriter writer =
new StreamWriter(
@"C:\Files\Data.txt"))
{
writer.WriteLine(
"Hello World");
}
The file automatically closes after use.
This is the preferred approach.
StreamReader reads file content.
Example:
StreamReader reader =
new StreamReader(
@"C:\Files\Data.txt");
string content =
reader.ReadToEnd();
reader.Close();
Output:
Hello World
StreamReader is commonly used for file processing.
Example:
using(StreamReader reader =
new StreamReader(
@"C:\Files\Data.txt"))
{
string content =
reader.ReadToEnd();
Console.WriteLine(
content);
}
This ensures proper resource cleanup.
Example:
using(StreamReader reader =
new StreamReader(
@"C:\Files\Data.txt"))
{
string line;
while(
(line =
reader.ReadLine())
!= null)
{
Console.WriteLine(
line);
}
}
Useful when processing large files.
FileInfo provides detailed information about files.
Example:
FileInfo file =
new FileInfo(
@"C:\Files\Data.txt");
Console.WriteLine(
file.Name);
Console.WriteLine(
file.Length);
Output:
Data.txt
2048
Useful for file management applications.
Example:
DirectoryInfo directory =
new DirectoryInfo(
@"C:\Files");
Properties:
Name
CreationTime
Parent
Root
Provides advanced folder management features.
Example:
using(StreamWriter writer =
new StreamWriter(
"log.txt", true))
{
writer.WriteLine(
DateTime.Now +
" User Logged In");
}
Output:
Log Entry Created
Logging systems heavily use file handling.
Example:
File.WriteAllText(
"StudentReport.txt",
"Student Name: Rahul");
Output:
Report Generated
Educational software frequently generates reports.
Example:
if(File.Exists(path))
{
}
Ensures resources are released.
File operations can fail unexpectedly.
Example:
try
{
}
catch(IOException ex)
{
}
Store paths in configuration files.
Prevent resource leaks.
Can lock files.
May crash applications.
Makes maintenance difficult.
May consume excessive memory.
Can result in data loss.
Transaction Reports
Audit Logs
Backup Files
Invoices
Product Reports
Sales Logs
Patient Records
Medical Reports
Prescriptions
Student Reports
Attendance Sheets
Result Files
File Handling in C# is used extensively across industries.
File Handling in C# refers to creating, reading, writing, updating, and deleting files and directories.
System.IO
StreamWriter writes data to files.
StreamReader reads data from files.
File provides static methods, while FileInfo provides object-oriented file management.
Most applications need to store, process, and manage persistent data.
File Handling in C# allows applications to work with files and directories.
The System.IO namespace.
StreamWriter is used to write data to files.
StreamReader is used to read file data.
FileInfo provides detailed information and operations for specific files.
It enables data storage, reporting, logging, backups, and document management in software applications.
WhatsApp us