← Back to C# Course | Chapter 14: File I/O & Streams | Lesson 5 of 6

FileInfo

In this page:

  1. FileInfo

FileInfo

FileInfo represents a specific file and exposes instance properties like Length, Exists, and LastWriteTime, plus methods like Delete() and CopyTo(). Unlike the static File class, FileInfo caches some information after construction, which can be more efficient for repeated checks on the same file. It's the object-oriented counterpart to File's static methods.

Note: Use FileInfo when you need to inspect several properties of the same file — it avoids repeated filesystem calls that File's static methods make each time.

Example: FileInfo

markup
using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string path = "info.txt";
        File.WriteAllText(path, "some sample content");

        FileInfo info = new FileInfo(path);

        Console.WriteLine(info.Name);
        Console.WriteLine(info.Length);
        Console.WriteLine(info.Exists);
    }
}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.