Skip to main content

How to Get a Path Relative to a Folder with Path.GetRelativePath in C#

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

Path.GetRelativePath("photos", "photos/2026/city.jpg") returns 2026/city.jpg, the way from the photos folder to that file. For a path outside the folder, the result starts with .., one folder up.

A path such as photos/2026/city.jpg says where a file is from the current folder. Sometimes you need it from another folder instead, such as the part below photos. The .NET API reference: Returns a relative path from one path to another.

The first argument is the folder to start from. The reference on it: The source path the result should be relative to. This path is always considered to be a directory. The second is where to go, which the reference calls The destination path.

Syntax​

Syntax
string <result> = Path.GetRelativePath(<relativeTo>, <path>);
PartWhat it is
<relativeTo>The folder to start from. It's always treated as a folder, even when its name looks like a file's
<path>The file or folder to get to
<result>The way from <relativeTo> to <path>: the names going down, .. for each folder up, and . when both are the same folder

The Example​

A console app called RelativePath measures four paths from a folder named photos: a file in it, a file in a folder below it, a file in another folder, and the photos folder itself. None of them has to exist on disk.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n RelativePath
cd RelativePath
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/RelativePath/RelativePath.csproj:
Determining projects to restore...
Restored /Users/you/source/RelativePath/RelativePath.csproj (in 49 ms).
Restore succeeded.

dotnet new console -n RelativePath creates a folder named RelativePath with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or RelativePath.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First, the folder to start from:

Program.cs (the folder to start from)
// The folder that the other paths are measured from.
string photos = "photos";

Every result in this example starts from photos.

Then the four paths:

Program.cs (four paths to measure)
// A file in the folder, a file in a subfolder, a file outside it, and the folder itself.
Show("photos/beach.jpg");
Show("photos/2026/city.jpg");
Show("music/song.mp3");
Show("photos");

Each line calls Show, the small function below, with one path.

Then the function that does the work:

Program.cs (print each result)
void Show(string path) => Console.WriteLine($"{path,-22} -> {Path.GetRelativePath(photos, path)}");

Show prints the path, an arrow, and Path.GetRelativePath(photos, path). ,-22 gives the path at least 22 characters, left-aligned, so the arrows line up. The C# guide on field widths: Positive widths right-align the value, and negative widths left-align it.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
photos/beach.jpg -> beach.jpg
photos/2026/city.jpg -> 2026/city.jpg
music/song.mp3 -> ../music/song.mp3
photos -> .

macOS-style terminal window titled Terminal — RelativePath showing dotnet run printing photos/beach.jpg -> beach.jpg, photos/2026/city.jpg -> 2026/city.jpg, music/song.mp3 -> ../music/song.mp3 and photos -> .

From photos, the file in it is just beach.jpg, and the file one folder down is 2026/city.jpg. music/song.mp3 isn't inside photos, so its result starts with ..: up one folder, out of photos, and then down into music. The folder itself came back as ., the same folder.

The Complete File​

macOS-style code window titled Program.cs — RelativePath showing the photos folder name, four calls to a Show function with a file in photos, a file in photos/2026, a file in music and photos itself, and the Show function that prints each path next to Path.GetRelativePath of photos and that path

Program.cs
// The folder that the other paths are measured from.
string photos = "photos";

// A file in the folder, a file in a subfolder, a file outside it, and the folder itself.
Show("photos/beach.jpg");
Show("photos/2026/city.jpg");
Show("music/song.mp3");
Show("photos");

void Show(string path) => Console.WriteLine($"{path,-22} -> {Path.GetRelativePath(photos, path)}");

Common Mistakes​

MistakeWhat happensDo this instead
Swapping the two argumentsPath.GetRelativePath("photos", "photos/2026/city.jpg") printed 2026/city.jpg, but Path.GetRelativePath("photos/2026/city.jpg", "photos") printed ../.., the way back upPut the folder you start from first, and the path you want to reach second
Passing a file as the folder to start fromPath.GetRelativePath("photos/beach.jpg", "photos/city.jpg") printed ../city.jpg, as if beach.jpg were a folderPass the file's folder. With Path.GetDirectoryName("photos/beach.jpg") as the first argument, the result was city.jpg
Starting from an empty folder nameThe app stopped with Unhandled exception. System.ArgumentException: The path is empty. (Parameter 'relativeTo'). The reference lists that exception for when relativeTo or path is effectively empty.For the current folder, pass .. Path.GetRelativePath(".", "photos/beach.jpg") printed photos/beach.jpg

FAQ​

Do the folders have to exist?​

No. A check printed photos exists: False and music exists: False, and Path.GetRelativePath("photos", "photos/2026/city.jpg") still returned 2026/city.jpg. The method works out the result from the paths. The reference: Paths are resolved by calling the GetFullPath method before calculating the difference.

Does letter case matter?​

It depends on the system. The reference: The method uses the default file path comparison for the current platform (StringComparison.OrdinalIgnoreCase for Windows and macOS, StringComparison.Ordinal for Linux). On macOS, Path.GetRelativePath("Photos", "photos/beach.jpg") printed beach.jpg: Photos and photos counted as the same folder.

Conclusion​

Path.GetRelativePath(folder, path) gives the way from a folder to a file or another folder: the names going down, .. for each step up, and . for the folder itself. Put the folder first, pass a folder and not a file, and don't pass an empty string. The paths don't have to exist on disk.

Additional Resources​