ZetCode

C# Path

last modified July 5, 2023

C# Path tutorial shows how to work with file and directory path information in C#. The operations are performed in a cross-platform manner. C# tutorial is a comprehensive tutorial on C# language.

The Path is located in the System.IO namespace. With the Path class, we can easily figure out the root path, the directory name of the file, its extension or create a random file name.

C# Path.GetPathRoot

The Path.GetPathRoot method returns the root directory information from the path contained in the specified character span.

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

Console.WriteLine(root);

The example prints the root path of the specified path.

$ dotnet run
/

C# Path.GetDirectoryName

The Path.GetDirectoryName returns the directory information for the specified path represented by a character span.

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

Console.WriteLine(dirName);

The example prints the directory name of the specified path.

$ dotnet run
/home/janbodnar

C# Path.GetFullPath

The Path.GetFullPath returns the absolute path for the specified path string.

Program.cs
var path = ".";

var fullPath = Path.GetFullPath(path);

Console.WriteLine(fullPath);

The example prints the full path of the current working directory.

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

C# Path.GetRandomFileName

The Path.GetRandomFileName returns a random directory or file name.

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

Console.WriteLine(Path.GetTempPath());

The example prints an example of a randomly generated file name.

$ dotnet run
j1wtvfxj.zrh

C# Path filename and extension

The Path.GetExtension returns the extension (including the period) of the specified path string. The Path.GetFileName returns the file name and extension of a file path represented by a read-only character span. The Path.GetFileNameWithoutExtension returns the file name without the extension of a file path represented by a read-only character span.

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);

The example prints the extension, the file name, and the file name without the extension of the specified path.

$ dotnet run
.txt
words.txt
words

C# Path.Combine

The Path.Combine combines strings into a path.

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);

The example concatenates individual strings into a single string that represents a file path.

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

Source

Path class - language reference

In this article we have worked with path strings in C#.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all C# tutorials.