C# Path GetExtension() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

In C#, Path.GetExtension() returns the extension of a path, including the leading dot. For "/data/report.txt" the answer is ".txt". If the file name has no extension, the answer is an empty string.

The method reads the text from the last dot to the end of the file name. A dot inside a folder name does not count, and only the last dot is used, so archive.tar.gz gives .gz. The case is kept as written.

This is a static method of System.IO.Path. Call it as Path.GetExtension(...); there is no new Path(). Like most Path methods, it only works with text: it never opens, reads, or creates a file, and it works the same whether or not the path exists.

Syntax

Pass a file name or a path. A null path returns null.

C#
string ext = Path.GetExtension("/data/report.txt");

Console.WriteLine(ext);                       // .txt
Console.WriteLine(ext.TrimStart('.'));        // txt
CallWhat it does
GetExtension("report.txt")Returns .txt.
GetExtension("archive.tar.gz")Returns .gz, the last extension only.
GetExtension("Makefile")No dot. Returns an empty string.
GetExtension("file.")A dot with nothing after it. Returns an empty string.
GetExtension(".gitignore")Returns .gitignore, because the last dot starts the name.

To compare extensions, ignore the case: string.Equals(ext, ".jpg", StringComparison.OrdinalIgnoreCase). ".JPG" and ".jpg" are the same kind of file for most programs.

Library declarations
C#
public static string? GetExtension(string? path);
public static ReadOnlySpan<char> GetExtension(ReadOnlySpan<char> path);

Parameters

ParameterDescription
pathA path or a file name. It does not need to exist.

Return Value

Return ValueDescription
string?The extension with its leading dot, or an empty string if there is none. null if path is null.

C# Examples

Example 1 — Read the Extension of Five Names

Normal files, a name with two dots, a name without a dot, and a folder name that contains a dot.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        Console.WriteLine(Path.GetExtension("report.txt"));
        Console.WriteLine(Path.GetExtension("archive.tar.gz"));
        Console.WriteLine("[" + Path.GetExtension("Makefile") + "]");
        Console.WriteLine("[" + Path.GetExtension("/data.v1/readme") + "]");
        Console.WriteLine(Path.GetExtension("photo.JPG"));
    }
}

How It Works

  1. Include the dot. The result for report.txt is .txt. The dot is always part of the answer.
  2. The last dot only. archive.tar.gz has two dots. The method returns .gz, the part after the last one.
  3. No dot, no extension. Makefile has no dot, so the answer is an empty string. The brackets show the empty text.
  4. A dot in a folder is skipped. The dot in data.v1 comes before the last separator, so readme has no extension.
  5. Case is kept. photo.JPG returns .JPG in capitals, exactly as written.

Example 2 — Group Files by Their Extension

Use the extension in lower case to decide what kind of file a name probably is.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string[] names = { "photo.JPG", "notes.txt", "data.bin", "README" };

        foreach (string name in names) {
            string ext = Path.GetExtension(name).ToLowerInvariant();
            string kind;

            switch (ext) {
                case ".jpg":
                case ".png":
                    kind = "image";
                    break;
                case ".txt":
                case ".md":
                    kind = "text";
                    break;
                case "":
                    kind = "no extension";
                    break;
                default:
                    kind = "other";
                    break;
            }

            Console.WriteLine(name + ": " + kind);
        }
    }
}

How It Works

  1. Lower case first. ToLowerInvariant() turns .JPG into .jpg, so one case label catches all spellings.
  2. Several labels, one result. .jpg and .png fall through to the same image branch, and .txt and .md share the text branch.
  3. The empty string is a value. README has no extension, so GetExtension returns "" and the case "" branch runs.
  4. Everything else. .bin matches no label and ends in default. The loop prints the original name, so you can see the case that was changed only for the test.

Example 3 — null, Dot Files, and a Trailing Dot

Corner cases that surprise beginners, plus the easy way to drop the dot.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        Console.WriteLine(Path.GetExtension(".gitignore"));
        Console.WriteLine("[" + Path.GetExtension("file.") + "]");

        string missing = null;
        Console.WriteLine(Path.GetExtension(missing) == null);

        string ext = Path.GetExtension("report.txt");
        Console.WriteLine(ext.TrimStart('.'));
        Console.WriteLine(string.Equals(ext, ".TXT", StringComparison.OrdinalIgnoreCase));
    }
}

How It Works

  1. A dot file. The last dot of .gitignore is its first character, so the whole name is the extension and is printed with its dot.
  2. A trailing dot. The dot at the end has nothing after it, so the extension is empty. The brackets show that the result is an empty string.
  3. null in, null out. A null path gives null and no exception, so the comparison is True.
  4. Drop the dot and ignore case. TrimStart('.') gives txt. OrdinalIgnoreCase says that .txt and .TXT are equal, so the last line is True.

Frequently Asked Questions

Call Path.GetExtension(path). It returns the extension with a leading dot, such as ".txt", or an empty string when there is none.
Yes. The result for report.txt is ".txt". Call TrimStart('.') on it if you want just "txt".
It returns ".gz". Only the text after the last dot is the extension. If you need ".tar.gz", check the name with EndsWith instead.
Use string.Equals(ext, ".jpg", StringComparison.OrdinalIgnoreCase), or call ToLowerInvariant on the extension first. GetExtension keeps the case exactly as written in the path.
.NET treats everything after the last dot as the extension, and the last dot in ".gitignore" is the first character. The result is ".gitignore" and the stem is empty.

Next: GetFileName()

Get the last part of a path: the file name with its extension.

GetFileName() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful