C# Path Methods

About This Index
This page is the C# Path Methods index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs. Path methods work mostly on text, so most examples need no files at all. The few that touch the disk use a demo file or folder in Path.GetTempPath() and delete it at the end.
Learn what the Path class is for on /c-sharp/path. Use this page when you need a specific API such as Combine(), GetFileName(), or GetFullPath().
Windows uses \ between folders and roots such as C:\. Linux and macOS use / and a root of /. The examples give forward-slash input (Windows accepts it too) and print results through Replace('\\', '/'), so the output is the same everywhere. Where a method really behaves differently, the output block says Output on Windows and Output on Linux/macOS.
How to Call a Path Method
Every Path method is static, so you call it on the class name. Add using System.IO; at the top of the file:
string file = Path.Combine("data", "reports", "2026.txt"); // build a path
string name = Path.GetFileName(file); // 2026.txt
string stem = Path.GetFileNameWithoutExtension(file); // 2026
string ext = Path.GetExtension(file); // .txt
string dir = Path.GetDirectoryName(file); // data/reports (or data\reports)
bool rooted = Path.IsPathRooted(file); // false: it is relative
string full = Path.GetFullPath(file); // adds the working directory C# Path Methods Table
All method guides in this C# tutorial series (20 tutorials), grouped by purpose.
Build / change
4 methodsGlue path pieces together with the right separator, swap an extension, or trim a trailing separator. These methods only build new text.
| Method | Description | Tutorial |
|---|---|---|
Combine() | Join folders and a file name into one path, with the right separator for your system. | Try it |
Join() | Glue path pieces together and keep every piece, even when one of them starts with a separator. | Try it |
ChangeExtension() | Return a copy of a path with a new extension, or with the extension removed. | Try it |
TrimEndingDirectorySeparator() | Remove one trailing separator from a path, but never from a root such as / or C:\. | Try it |
Take apart
6 methodsRead one part of a path: the folder, the file name, the extension, or the root. Nothing has to exist on disk.
| Method | Description | Tutorial |
|---|---|---|
GetDirectoryName() | Get the folder part of a path: everything before the last separator. | Try it |
GetFileName() | Get the last part of a path: the file name with its extension. | Try it |
GetFileNameWithoutExtension() | Get the file name without its last extension: report.txt becomes report. | Try it |
GetExtension() | Get the extension of a file name, including the leading dot. | Try it |
HasExtension() | Check whether the file name part of a path has an extension. | Try it |
GetPathRoot() | Get the root of a path: the / on Linux and macOS, or a drive such as C:\ on Windows. | Try it |
Check
3 methodsAsk a yes or no question about a path: does something exist there, is it rooted, is it fully qualified.
| Method | Description | Tutorial |
|---|---|---|
Exists() | Ask whether a file or a folder exists at a path. It never throws. | Try it |
IsPathRooted() | Check whether a path starts from a root (like / or C:\) instead of from the working folder. | Try it |
IsPathFullyQualified() | Check whether a path points to one exact place, independent of the working folder and drive. | Try it |
Full / relative
2 methodsTurn a short path into a full one, or a full path into the shortest relative path between two places.
| Method | Description | Tutorial |
|---|---|---|
GetFullPath() | Turn a relative path into a full one, and clean up dot segments such as .. along the way. | Try it |
GetRelativePath() | Get the shortest relative path from one folder to a file or folder, using .. steps when needed. | Try it |
Temp / random
3 methodsFind the temp folder, make a unique temp file, or get a random name. Only GetTempFileName creates a real file.
| Method | Description | Tutorial |
|---|---|---|
GetTempPath() | Get the path of the temp folder of the current user or system, with a trailing separator. | Try it |
GetTempFileName() | Create a unique empty file in the temp folder and get its path. You must delete it yourself. | Try it |
GetRandomFileName() | Get a random, cryptographically strong 8.3 name such as abcdefgh.xyz. No file is created. | Try it |
Invalid characters
2 methodsList the characters that this operating system does not allow in a file name or a path, so you can check or clean user input.
| Method | Description | Tutorial |
|---|---|---|
GetInvalidFileNameChars() | Get the characters that are not allowed in a file name on this operating system. | Try it |
GetInvalidPathChars() | Get the characters that are not allowed anywhere in a path on this operating system. | Try it |
Frequently Asked Questions
Start with Combine()
Join folders and a file name with the right separator, and learn the one trap with rooted pieces.
12 people found this page helpful
