Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Files and I/O

The Path class

Reference for System.IO.Path in C#: Combine and Join, GetFileName, GetExtension, GetFullPath, GetRelativePath, ChangeExtension, temp paths and path security.

System.IO.Path is a static class that builds and takes apart file and directory paths as strings. It does not touch the disk: Path.Combine("a", "b.txt") works even if neither exists. Using Path instead of string concatenation gets the directory separators right on every operating system (a slash on Linux and macOS, a backslash on Windows) and removes a whole class of bugs with doubled or missing separators.

The most used members are Combine and Join (build a path), GetFileName, GetFileNameWithoutExtension, GetExtension and GetDirectoryName (take it apart), GetFullPath and GetRelativePath (convert between absolute and relative), and GetTempPath (a place for temporary files).

C#
string path = Path.Combine("data", "2026", "report.csv");
Console.WriteLine(Path.GetFileNameWithoutExtension(path));   // report

Try it

C# Take a path apart and build a new one

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Full, relative and rooted paths

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static string Combine(string path1, string path2);          // also overloads for 3, 4 and params string[]
public static string Join(string? path1, string? path2);           // same overload shapes
public static string? GetDirectoryName(string? path);
public static string GetFileName(string? path);
public static string GetFileNameWithoutExtension(string? path);
public static string GetExtension(string? path);
public static string? ChangeExtension(string? path, string? extension);
public static string GetFullPath(string path);
public static string GetFullPath(string path, string basePath);
public static string GetRelativePath(string relativeTo, string path);
public static bool IsPathRooted(string? path);
public static bool IsPathFullyQualified(string path);
public static string GetTempPath();
public static string GetTempFileName();
public static string GetRandomFileName();
public static char[] GetInvalidFileNameChars();
Member Description
Combine(a, b, ...) Joins segments with the platform separator; a later rooted segment replaces everything before it.
Join(a, b, ...) Joins segments with a separator and never discards anything, so it is the right choice for untrusted segments.
GetFileName(path) The part after the last separator.
GetDirectoryName(path) Everything before the last separator, or null for a root.
GetExtension(path) The last .xyz including the dot, or an empty string.
ChangeExtension(path, ext) Replaces the extension; null removes it; a missing dot is added.
GetFullPath(path[, base]) Absolute, normalized path: . and .. are resolved. Does not check existence.
GetRelativePath(from, to) Relative path from one folder to another path.
GetTempFileName() Creates an empty file in the temp folder and returns its name.
GetRandomFileName() A random 8.3-style name; creates nothing.

Errors: ArgumentNullException for a null argument in Combine; ArgumentException for characters that are invalid in paths in the path-validating methods (on Windows; Linux rejects only the null character).

Usage notes

  • Always use Path to build paths. Concatenating dir + "/" + name breaks on Windows-style paths and when dir already ends with a separator.
  • Combine throws away earlier parts when a later argument starts with a separator: Path.Combine("data", "/etc") is /etc. With user-supplied segments this is a security hole; use Join, or resolve with GetFullPath and check that the result starts with the allowed base directory, as the example shows.
  • GetFullPath and GetRelativePath use the current directory for a relative input unless you pass a base. They never check whether the file exists.
  • Dot files. GetExtension("/x/.gitignore") returns .gitignore, and GetFileNameWithoutExtension returns an empty string, so a leading dot counts as an extension separator.
  • Only the last extension counts: archive.tar.gz has the extension .gz.
  • Separators differ by OS, but both are accepted on Windows. On Linux, a backslash is an ordinary file-name character, so Path.GetFileName(@"C:\dir\file.txt") returns the whole string. Normalize paths that come from other systems before parsing them.
  • GetTempFileName creates a file (and may fail when the temp folder is full or has too many files); delete it when you are done. For a name without a file, use Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()), or create a whole folder with Directory.CreateTempSubdirectory.
  • Span overloads. Most methods accept ReadOnlySpan<char> and avoid allocations (GetFileName(ReadOnlySpan<char>)).
  • Long paths (over 260 characters) work on current .NET on Windows 10 and later when the OS setting allows them; older code needed a special long-path prefix (two backslashes, a question mark and another backslash) in front of the path.
  • Path does not sanitize. GetInvalidFileNameChars lists what the current OS forbids; for portable names, also avoid reserved Windows names like CON and trailing dots or spaces.