File vs FileInfo in C#: Key Differences and When to Use Each
File is a static class providing quick, simple file operations without creating objects, while FileInfo is an instance class that represents a specific file and offers more detailed file management with better performance for repeated operations. Use File for simple, one-time tasks and FileInfo when working with the same file multiple times.Quick Comparison
This table summarizes the main differences between File and FileInfo classes in C#.
| Aspect | File | FileInfo |
|---|---|---|
| Type | Static class | Instance class |
| Usage | Direct static method calls | Create object representing a file |
| Performance | Slower for multiple operations on same file | Faster for repeated operations on same file |
| File Representation | No file object, just methods | Represents a specific file with properties |
| Common Methods | ReadAllText, WriteAllText, Delete | Open, CopyTo, MoveTo, Delete |
| When to Use | Simple, one-off file tasks | Complex or repeated file operations |
Key Differences
The File class is static, meaning you don't create an object to use it. You just call its methods directly to perform quick file operations like reading or writing text. This makes it simple and convenient for one-time tasks.
On the other hand, FileInfo is an instance class. You create an object that represents a specific file, which lets you access file properties like size or creation date easily. Because it holds state about the file, it performs better when you do many operations on the same file, avoiding repeated lookups.
Also, FileInfo provides more detailed control and methods for file management, such as moving or copying files with instance methods. Meanwhile, File offers similar functionality but only through static methods without storing file info.
Code Comparison
using System; using System.IO; class Program { static void Main() { string path = "example.txt"; // Write text to file using File File.WriteAllText(path, "Hello from File class!"); // Read text from file using File string content = File.ReadAllText(path); Console.WriteLine(content); // Delete file using File File.Delete(path); } }
FileInfo Equivalent
using System; using System.IO; class Program { static void Main() { string path = "example.txt"; FileInfo fileInfo = new FileInfo(path); // Write text to file using FileInfo using (StreamWriter writer = fileInfo.CreateText()) { writer.WriteLine("Hello from FileInfo class!"); } // Read text from file using FileInfo using (StreamReader reader = fileInfo.OpenText()) { string content = reader.ReadToEnd(); Console.WriteLine(content); } // Delete file using FileInfo fileInfo.Delete(); } }
When to Use Which
Choose File when you need to perform quick, simple file operations like reading or writing text once, without managing file state. It is easy and straightforward for one-off tasks.
Choose FileInfo when you work repeatedly with the same file, need to access file properties, or perform multiple operations like moving, copying, or deleting. It offers better performance and more control in these cases.
Key Takeaways
File for simple, one-time file operations with static methods.FileInfo when working multiple times with the same file for better performance.FileInfo provides detailed file properties and instance methods.File is easier for quick tasks but less efficient for repeated access.FileInfo for complex file management and File for straightforward tasks.