How to Get File Extension in C#

Alexey Karimov

Ever needed to check a file’s type before processing or validating it in your C# application? Whether you’re handling uploads, filtering files, or logging file operations, knowing a file’s extension is often the first step. If you’ve found yourself in this situation, you might want to know how to get a file extension in C#.

A file extension represents the suffix at the end of a file name that indicates its format, such as .txt, .pdf, or .jpg. It helps your application identify how the file should be handled or displayed. In C#, the System.IO namespace provides reliable methods for retrieving this information, both for quick string-based checks and for more in-depth file object operations.

In this guide, we’ll explore how to get a file’s extension in C# using Path.GetExtension and FileInfo.Extension, along with best practices to handle edge cases cleanly.

Using Path.GetExtension

The simplest and most common way to get a file’s extension in C# is by using the Path.GetExtension() method from the System.IO namespace. This method takes a file path as input and returns the file extension, including the period. If the path doesn’t have an extension, it returns an empty string. If the path is null, it returns null.

You can think of this method as a quick lookup tool that checks the text in the file path, finds the last period before any directory separator, and returns everything after it. It doesn’t read or open the file, so it’s fast and lightweight.

Here’s a simple example:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = @"C:\Projects\report.pdf";
        string extension = Path.GetExtension(filePath);

        Console.WriteLine("File Extension: " + extension);
    }
}

Output: File Extension: .pdf

Let’s look at a few quick rules to understand how Path.GetExtension behaves:

  • If the path is @”C:\Docs\file.txt”, it returns .txt.
  • If the path is @”C:\Docs\file”, it returns an empty string.
  • If the path ends with a backslash or refers to a folder, it returns an empty string.
  • If the file name contains multiple dots, such as archive.backup.tar.gz, it returns only .gz.

The method is purely string-based, making it ideal when you don’t need extra file information, such as size, creation date, or permissions.

Using FileInfo.Extension

Another way to get a file’s extension in C# is by using the Extension property of the FileInfo class. This method is part of the same System.IO namespace but works a little differently from Path.GetExtension. Instead of working on a plain text path, it operates on a FileInfo object, which represents a specific file on your system.

When you create a FileInfo instance, you provide the full path of a file. The class then lets you access multiple properties about that file, including its name, directory, size, and, of course, its extension.

Here’s how it works:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        FileInfo file = new FileInfo(@"C:\Projects\image.png");

        Console.WriteLine("File Name: " + file.Name);
        Console.WriteLine("Extension: " + file.Extension);
    }
}

Output:

File Name: image.png
Extension: .png

The Extension property returns the file’s extension, including the dot, just like Path.GetExtension. If the file has no extension, it returns an empty string. Since FileInfo is object-based, it becomes useful when you already have a file reference and need more details about it, such as its creation date or size.

It’s also important to know that the FileInfo class does not need to open or read the file to return this information. It simply interprets the file path provided to it. This means it’s both safe and efficient for checking extensions in real-world scenarios.

Wrapping Up

When working with file paths, always check for null or empty results before using an extension value. If you need to compare extensions, convert them using ToLowerInvariant() to avoid case issues. 

Remember, a file extension only describes the name, not the actual content, so verify the file type when accuracy matters. Also, validate input paths to prevent ArgumentException from invalid characters, and use try-catch when handling user-provided paths or uploads.

In short, use Path.GetExtension for quick path-based checks and FileInfo.Extension when working with file objects. Both make retrieving extensions simple, reliable, and efficient in C#.