ZetCode

C# Path

last modified April 22, 2025

This tutorial demonstrates how to manage file and directory paths in C# using the Path class, ensuring cross-platform compatibility. Explore our comprehensive C# tutorial for in-depth language insights.

The Path class, found in the System.IO namespace, provides methods to extract root paths, directory names, file extensions, and generate random file names effortlessly.

C# Path.GetPathRoot

The Path.GetPathRoot method retrieves the root directory of a specified path, enabling consistent path handling across platforms.

Program.cs
var path = "/home/janbodnar/tmp/";
var root = Path.GetPathRoot(path);

Console.WriteLine(root);

This example outputs the root directory of the provided path, illustrating basic path parsing.

$ dotnet run
/

C# Path.GetDirectoryName

The Path.GetDirectoryName method extracts the directory portion of a path, useful for navigating file system structures.

Program.cs
var path = "/home/janbodnar/words.txt";
var dirName = Path.GetDirectoryName(path);

Console.WriteLine(dirName);

This example displays the directory name of the specified file path, demonstrating directory extraction.

$ dotnet run
/home/janbodnar

C# Path.GetFullPath

The Path.GetFullPath method converts a relative path to an absolute path, resolving references like "." or "..".

Program.cs
var path = ".";

var fullPath = Path.GetFullPath(path);

Console.WriteLine(fullPath);

This example outputs the absolute path of the current working directory, showcasing path resolution.

$ dotnet run
/home/janbodnar/Documents/prog/c#/path/FullPath

C# Path.GetRandomFileName

The Path.GetRandomFileName method generates a random file or directory name, ideal for temporary file creation.

Program.cs
var randFileName = Path.GetRandomFileName();
Console.WriteLine(randFileName);

Console.WriteLine(Path.GetTempPath());

This example outputs a randomly generated file name, useful for creating unique temporary files.

$ dotnet run
j1wtvfxj.zrh

C# Path filename and extension

The Path.GetExtension method retrieves a file's extension, including the period. The Path.GetFileName method returns the file name with its extension, while Path.GetFileNameWithoutExtension excludes the extension.

Program.cs
var path = "/home/janbodnar/words.txt";

var fileExt = Path.GetExtension(path);
Console.WriteLine(fileExt);

var fileName = Path.GetFileName(path);
Console.WriteLine(fileName);

var fileNameWithoutExt = Path.GetFileNameWithoutExtension(path);
Console.WriteLine(fileNameWithoutExt);

This example extracts and displays the file extension, full file name, and file name without extension from a path.

$ dotnet run
.txt
words.txt
words

C# Path.Combine

The Path.Combine method concatenates strings into a valid file path, handling platform-specific separators automatically.

Program.cs
var fullPath1 = Path.Combine("/home", "janbodnar", "words.txt");
Console.WriteLine(fullPath1);

var fullPath2 = Path.Combine("/home/janbodnar/", "/home/janbodnar/words2.txt");
Console.WriteLine(fullPath2);

This example combines strings to form file paths, demonstrating robust path construction across platforms.

$ dotnet run
/home/janbodnar/words.txt
/home/janbodnar/words2.txt

C# Path.ChangeExtension

The Path.ChangeExtension method modifies a file's extension, allowing easy transformation of file types in path strings.

Program.cs
var path = "/home/janbodnar/words.txt";
var newPath = Path.ChangeExtension(path, "bak");

Console.WriteLine(newPath);

This example changes the file extension from .txt to .bak, illustrating extension modification.

$ dotnet run
/home/janbodnar/words.bak

C# Path.IsPathFullyQualified

The Path.IsPathFullyQualified method checks if a path is absolute, helping validate path inputs in applications.

Program.cs
var path1 = "/home/janbodnar/words.txt";
var path2 = "words.txt";

Console.WriteLine(Path.IsPathFullyQualified(path1));
Console.WriteLine(Path.IsPathFullyQualified(path2));

This example tests whether paths are fully qualified, returning true for absolute paths and false for relative ones.

$ dotnet run
True
False

C# Path.Join with span-based operations

The Path.Join method efficiently combines path segments using spans, offering a modern, memory-efficient alternative to Path.Combine.

Program.cs
var path = new char[100];
Path.Join(path, "/home".AsSpan(), "janbodnar".AsSpan(), "docs".AsSpan());

Console.WriteLine(path);

This example uses Path.Join with character spans to build a path, demonstrating efficient string manipulation for large-scale applications.

$ dotnet run
/home/janbodnar/docs

C# Path.GetRelativePath

The Path.GetRelativePath method computes the relative path from one directory to another, useful for file system navigation and comparisons.

Program.cs
var basePath = "/home/janbodnar/docs";
var targetPath = "/home/janbodnar/docs/projects/words.txt";

var relativePath = Path.GetRelativePath(basePath, targetPath);
Console.WriteLine(relativePath);

This example calculates the relative path from a base directory to a target file, simplifying path manipulation in file system operations.

$ dotnet run
projects/words.txt

Source

Path class - language reference

This article explored advanced techniques for handling path strings in C# using the Path class.

Author

I am Jan Bodnar, a dedicated programmer with extensive experience in software development. Since 2007, I have authored over 1,400 programming articles and eight e-books. With more than a decade of teaching programming, I share my expertise through comprehensive tutorials.

List all C# tutorials.