File Operations in C# – A Practical Guide
File Operations in C# – A Practical Guide
In C# and .NET, working with files is a common requirement. Whether you need to verify a file’s presence, read its contents, or manage file copies, the System.IO.File class offers straightforward, thread‑safe methods. Below you’ll find concise explanations and sample code that follows best practices and Microsoft’s official guidance.
Basic File Actions
- Reading – Retrieve data from an existing file.
- Writing – Overwrite the entire file with new content (the default behavior).
- Appending – Add data to the end of a file without deleting existing content.
Key Methods Covered
File.Exists– Check if a file is present.File.ReadAllLines– Load all lines into a string array.File.ReadAllText– Load the whole file into a single string.File.Copy– Duplicate a file to a new location.File.Delete– Remove a file from the file system.
Prerequisites
For all examples we assume a text file D:\Example.txt exists with the following two lines:
Guru99 – .Net Guru99 – C#
1. Checking File Existence – File.Exists
using System;
using System.IO;
namespace DemoApplication
{
class Tutorial
{
static void Main(string[] args)
{
string path = "D:\\Example.txt";
if (File.Exists(path))
{
Console.WriteLine("File Exists");
}
else
{
Console.WriteLine("File Not Found");
}
Console.ReadKey();
}
}
}
The program outputs File Exists when D:\Example.txt is present.
2. Reading Lines Individually – File.ReadAllLines
using System;
using System.IO;
namespace DemoApplication
{
class Tutorial
{
static void Main(string[] args)
{
string path = "D:\\Example.txt";
string[] lines = File.ReadAllLines(path);
foreach (var line in lines)
{
Console.WriteLine(line);
}
Console.ReadKey();
}
}
}
Each line from the file is printed on a separate console line.
3. Reading the Entire File – File.ReadAllText
using System;
using System.IO;
namespace DemoApplication
{
class Tutorial
{
static void Main(string[] args)
{
string path = "D:\\Example.txt";
string content = File.ReadAllText(path);
Console.WriteLine(content);
Console.ReadKey();
}
}
}
The console displays both lines concatenated with a line break.
4. Copying a File – File.Copy
using System;
using System.IO;
namespace DemoApplication
{
class Tutorial
{
static void Main(string[] args)
{
string source = "D:\\Example.txt";
string destination = "D:\\ExampleNew.txt";
File.Copy(source, destination, overwrite: true);
Console.WriteLine("Copy succeeded");
Console.ReadKey();
}
}
}
The new file D:\ExampleNew.txt contains an exact copy of Example.txt.
5. Deleting a File – File.Delete
using System;
using System.IO;
namespace DemoApplication
{
class Tutorial
{
static void Main(string[] args)
{
string path = "D:\\Example.txt";
File.Delete(path);
Console.WriteLine("File deleted");
Console.ReadKey();
}
}
}
After execution, D:\Example.txt is permanently removed from the D drive.
Summary
- The
System.IO.Fileclass provides essential file‑system operations. - Use
File.ReadAllTextorFile.ReadAllLinesto ingest file data into your application. - Always handle exceptions such as
FileNotFoundExceptionorUnauthorizedAccessExceptionin production code.
C Language
- Mastering C Input and Output (I/O): scanf() and printf() Explained
- C File Handling: Mastering I/O with fopen, fprintf, fread, and fseek
- Python File I/O: Mastering File Operations, Reading, Writing, and Management
- Java Input/Output Streams: Fundamentals and Types
- Process Automation I/O Systems: Market Trends, Decentralization, and Ethernet‑Based Field Connectivity
- Mastering C File I/O: Create, Open, Read, Write, and Close Files with Confidence
- Mastering File I/O in Verilog: Open, Read, Write, and Close
- Mastering File I/O in C: Creating, Opening, and Managing Files
- Mastering C# File I/O: Reading, Writing, and Managing Streams
- Python File I/O: Mastering Input and Output Operations