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

FileInfo

FileInfo is like a file's ID card that tells you its name, size, and dates, and lets you copy or delete it. It describes one specific file.

In this page:

  1. FileInfo
Syntax
csharp
FileInfo info = new FileInfo(path);
info.Name;
info.Length;
info.Exists;
info.LastWriteTime;

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);
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Reading info.Length for a file that does not exist, which throws a FileNotFoundException; check info.Exists first.
  2. Reusing a FileInfo after the file changed and reading stale values, since properties are cached until you call info.Refresh().
  3. Treating Length as the number of characters, when it is the file size in bytes.
🔒

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.