C# Path Methods

Beginner
6 min read
Updated: Nov 2026
20 method guides
Method index

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 and Linux write paths differently.

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:

C#
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 methods

Glue path pieces together with the right separator, swap an extension, or trim a trailing separator. These methods only build new text.

MethodDescriptionTutorial
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 methods

Read one part of a path: the folder, the file name, the extension, or the root. Nothing has to exist on disk.

MethodDescriptionTutorial
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 methods

Ask a yes or no question about a path: does something exist there, is it rooted, is it fully qualified.

MethodDescriptionTutorial
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 methods

Turn a short path into a full one, or a full path into the shortest relative path between two places.

MethodDescriptionTutorial
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 methods

Find the temp folder, make a unique temp file, or get a random name. Only GetTempFileName creates a real file.

MethodDescriptionTutorial
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 methods

List the characters that this operating system does not allow in a file name or a path, so you can check or clean user input.

MethodDescriptionTutorial
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

They are the static methods of System.IO.Path. They join path pieces, take a path apart into folder, file name, extension and root, check a path, make full or relative paths, and give temp and random names. Most of them only work with text.
Mostly no. Combine, Join, GetFileName, GetExtension and the other string helpers never open a file. The exceptions are Exists (it asks the file system), GetFullPath (it reads the working directory), GetTempFileName (it creates a 0-byte file), and GetTempPath (it reads an environment variable).
Windows joins paths with a backslash and Linux and macOS use a forward slash. When a method inserts the separator itself, the output would differ by system. Replace('\\', '/') turns every backslash into a slash, so the output you see is the output you get on any system.
Combine throws away everything before a rooted piece such as "/etc", so Combine("a", "/b") is "/b". Join never does that: it keeps both pieces and only adds a separator when one is missing. Use Combine for trusted paths and Join when you want a plain glue.
Read the C# Path hub first. Then open Combine(), GetFileName(), GetExtension(), and GetFullPath() from the tables below.

Start with Combine()

Join folders and a file name with the right separator, and learn the one trap with rooted pieces.

Combine() 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.

12 people found this page helpful