How to Get a Path Relative to a Folder with Path.GetRelativePath in C#
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
string <result> = Path.GetRelativePath(<relativeTo>, <path>);
| Part | What 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 --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n RelativePath
cd RelativePath
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:
// The folder that the other paths are measured from.
string photos = "photos";
Every result in this example starts from photos.
Then the four paths:
// 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:
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:
dotnet run
photos/beach.jpg -> beach.jpg
photos/2026/city.jpg -> 2026/city.jpg
music/song.mp3 -> ../music/song.mp3
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

// 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
| Mistake | What happens | Do this instead |
|---|---|---|
| Swapping the two arguments | Path.GetRelativePath("photos", "photos/2026/city.jpg") printed 2026/city.jpg, but Path.GetRelativePath("photos/2026/city.jpg", "photos") printed ../.., the way back up | Put the folder you start from first, and the path you want to reach second |
| Passing a file as the folder to start from | Path.GetRelativePath("photos/beach.jpg", "photos/city.jpg") printed ../city.jpg, as if beach.jpg were a folder | Pass the file's folder. With Path.GetDirectoryName("photos/beach.jpg") as the first argument, the result was city.jpg |
| Starting from an empty folder name | The 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
- Path.GetRelativePath Method — its two arguments, what it returns, and how it compares paths.
- Path.GetFullPath Method — how relative paths are resolved first.
- Path.GetDirectoryName Method — the folder part of a file's path.
- String interpolation — field widths such as
,-22.
