C# File Class: Static File Operations Explained
A practical guide to the C# File class: reading and writing files, using streams and async methods, handling errors, and choosing between File and FileInfo.

The File class in System.IO provides static methods for creating, copying, deleting, moving, and opening files. Unlike FileInfo, which requires an instance, File methods are called directly on the type. That makes the class a convenient choice for one-off path-based file operations, such as reading a configuration file or writing a log file. This article explains the most common file operations and the trade-offs to consider when choosing between the static methods and stream- or instance-based alternatives.
Reading Text Files with ReadAllText and ReadAllLines
For small to medium-sized text files, File.ReadAllText and File.ReadAllLines are the simplest entry points. ReadAllText returns the entire file as a single string, while ReadAllLines returns a string array where each element is one line. The default encoding is UTF-8, and the methods detect a byte order mark (BOM) when one is present.
string content = File.ReadAllText("config.json");
string[] lines = File.ReadAllLines("log.txt");These methods are convenient because they encapsulate opening, reading, and closing the file. However, they load the entire file into memory. For a file that is several hundred megabytes, this can cause high memory pressure. In such cases, consider using File.OpenText or a StreamReader to process lines incrementally.
Writing Text Files with WriteAllText and AppendAllText
File.WriteAllText creates a new file and writes the given string to it, overwriting the file if it already exists. File.AppendAllText adds text to the end of the file, creating it if necessary. Both methods accept an optional encoding parameter; the default is UTF-8 without a BOM.
File.WriteAllText("output.txt", "Hello, world!");
File.AppendAllText("output.txt", "\nSecond line");These static methods are convenient because each call opens, writes, and closes the file in a single operation. They are not a good fit for high-frequency writes, because every call opens and closes the file handle. For repeated appends, keep a StreamWriter open for the duration of the operation instead.
Checking File Existence and Attributes
File.Exists returns true if the specified file exists and the caller has permission to read it. It returns false for a null or empty path, and it also returns false if the path is a directory. File.GetAttributes returns a FileAttributes enum value that can be used to check for ReadOnly, Hidden, System, or Directory flags.
if (File.Exists("data.csv"))
{
FileAttributes attrs = File.GetAttributes("data.csv");
bool isReadOnly = (attrs & FileAttributes.ReadOnly) != 0;
}A common mistake is to call File.Exists immediately before reading or writing and then assume the file is still present. The file system can change between the check and the operation, leading to a FileNotFoundException. It is safer to attempt the operation and catch the exception, or to open the file handle once and use that handle for subsequent operations.
Working with Streams: OpenRead, OpenWrite, and FileStream
When you need more control over reading or writing, File.OpenRead returns a read-only FileStream, and File.OpenWrite returns a write-only FileStream. These methods let you read or write in chunks, seek to specific positions, and manage buffer size.
using (FileStream fs = File.OpenRead("large.bin"))
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
{
// Process the chunk
}
}File.OpenWrite opens an existing file for writing or creates a new one. It does not append; writing starts at the beginning of the file. Use File.Open with FileMode.Append when you need to append. The FileStream returned by these methods is IDisposable, so a using statement is required to release the handle promptly.
Async File Operations: ReadAllTextAsync and WriteAllTextAsync
For I/O-bound operations, common async overloads are available: ReadAllTextAsync, WriteAllTextAsync, ReadAllLinesAsync, and AppendAllTextAsync. These methods release the calling thread while file I/O is in progress, which is useful in UI applications or high-concurrency web servers.
string content = await File.ReadAllTextAsync("data.json");
await File.WriteAllTextAsync("result.txt", content);Async methods do not improve disk throughput; they improve thread utilization. For a one-off file operation in a console app, the overhead of async may not be worth it. Use async when the application needs to handle many concurrent file operations or when the calling thread is a UI thread that should not block.
File vs FileInfo: Choosing the Right Abstraction
File is a static class, while FileInfo is an instance class that represents a specific file. FileInfo is useful when you need to perform multiple operations on the same file, because it caches the path and provides instance methods like CopyTo, MoveTo, and Delete. It also exposes properties such as Length, LastWriteTime, and Attributes without re-parsing the path.
| Criterion | File (static) | FileInfo (instance) |
|---|---|---|
| Usage | One-off operations | Repeated operations on same file |
| Path handling | Passed to each method | Stored in the instance |
| Property access | Requires File.Get* calls | Direct properties |
| Best fit | Quick read/write | Multiple checks or operations |
For example, if you need to check a file's length and then read its contents, FileInfo avoids calling File.GetLength and File.ReadAllText with the same string path twice. But for a single read, the static File method is simpler.
Error Handling and File Sharing Considerations
File operations can fail for many reasons: the file does not exist, the caller lacks permission, the file is locked by another process, or the path is invalid. The File class throws specific exceptions that you should handle based on the context.
FileNotFoundExceptionwhen the file is missing.UnauthorizedAccessExceptionwhen access is denied.IOExceptionfor generic I/O errors, including sharing violations.ArgumentException,NotSupportedException, orPathTooLongExceptionfor invalid paths.
When another process holds an exclusive lock, File.OpenRead can throw IOException. To control sharing behavior, use File.Open with a FileShare value. For example, FileShare.ReadWrite allows other processes to read and write while the file is open, but this can lead to data corruption if the processes are not coordinated.
using (FileStream fs = new FileStream("shared.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
// Read while allowing others to read/write
}For production code, avoid catching Exception broadly. Instead, catch the specific exceptions that are expected for the operation and let unexpected ones propagate. Also, consider using FileOptions.Asynchronous when creating a FileStream if you plan to use async methods; the static async methods handle this internally.
A final note on performance: static read/write methods such as ReadAllText, WriteAllText, and AppendAllText open and close the file handle with each call. If you perform many operations on the same file, reuse a single FileStream or StreamReader/StreamWriter instance. For large files, always prefer streaming over loading the entire content into memory. And remember that File.Exists is not a guarantee for subsequent operations; handle the exceptions that may occur instead of relying on a pre-check.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.