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.
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
Parameter
Description
path
A path or a file name. It does not need to exist.
Return Value
Return Value
Description
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"));
}
}
Output
.txt
.gz
[]
[]
.JPG
How It Works
Include the dot. The result for report.txt is .txt. The dot is always part of the answer.
The last dot only.archive.tar.gz has two dots. The method returns .gz, the part after the last one.
No dot, no extension.Makefile has no dot, so the answer is an empty string. The brackets show the empty text.
A dot in a folder is skipped. The dot in data.v1 comes before the last separator, so readme has no extension.
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);
}
}
}
Output
photo.JPG: image
notes.txt: text
data.bin: other
README: no extension
How It Works
Lower case first.ToLowerInvariant() turns .JPG into .jpg, so one case label catches all spellings.
Several labels, one result..jpg and .png fall through to the same image branch, and .txt and .md share the text branch.
The empty string is a value.README has no extension, so GetExtension returns "" and the case "" branch runs.
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));
}
}
Output
.gitignore
[]
True
txt
True
How It Works
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.
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.
null in, null out. A null path gives null and no exception, so the comparison is True.
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.