---
title: "How do you get a file extension in C#?"
description: "Use Path.GetExtension to read a file's extension in C#, with GetFileNameWithoutExtension and ChangeExtension for related tasks. Includes case handling."
url: "https://bugsee.com/answers/dotnet/how-to-get-file-extension-in-c/"
---

# How do you get a file extension in C#?

`Path.GetExtension("folder/report.pdf")` returns `".pdf"` including the dot, and an empty string when there is no extension.

```csharp
using System.IO;

string path = @"C:\data\Report.Final.PDF";

Path.GetExtension(path);                    // ".PDF"
Path.GetFileName(path);                     // "Report.Final.PDF"
Path.GetFileNameWithoutExtension(path);     // "Report.Final"
Path.ChangeExtension(path, ".txt");         // C:\data\Report.Final.txt

bool isPdf = string.Equals(
    Path.GetExtension(path), ".pdf", StringComparison.OrdinalIgnoreCase);
```

Only the part after the last dot counts, and file names such as `.gitignore` or `archive.tar.gz` give `.gitignore` and `.gz`. Compare extensions without regard to case, and do not trust an extension to prove what a file contains.

## Related questions

- [How do properties with get and set work in C#?](https://bugsee.com/answers/dotnet/c-properties-get-and-set-explained/)
