0
0
CsharpComparisonBeginner · 3 min read

File vs FileInfo in C#: Key Differences and When to Use Each

In C#, 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#.

AspectFileFileInfo
TypeStatic classInstance class
UsageDirect static method callsCreate object representing a file
PerformanceSlower for multiple operations on same fileFaster for repeated operations on same file
File RepresentationNo file object, just methodsRepresents a specific file with properties
Common MethodsReadAllText, WriteAllText, DeleteOpen, CopyTo, MoveTo, Delete
When to UseSimple, one-off file tasksComplex 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

csharp
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);
    }
}
Output
Hello from File class!
↔️

FileInfo Equivalent

csharp
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();
    }
}
Output
Hello from FileInfo class!
🎯

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

Use File for simple, one-time file operations with static methods.
Use 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.
Pick FileInfo for complex file management and File for straightforward tasks.