In C#, Path.GetRelativePath(relativeTo, path) returns the relative path you would follow to get from the folder relativeTo to path. From "/a/b" to "/a/c/d.txt" the answer is "../c/d.txt": one step up, then down into c.
Both arguments are first made full with GetFullPath, so they may be relative too. The result uses the separator of the current system, and it works on text only. It is available in .NET Core 2.0 and newer (and .NET Standard 2.1).
This is a static method of System.IO.Path. Call it as Path.GetRelativePath(...); there is no new Path(). Like most Path methods, it only works with text: it never opens, reads, or creates a file, and it works the same whether or not the path exists.
Syntax
Pass the starting folder first, then the target. The order matters.
Returns ../c/d.txt (a backslash style on Windows).
GetRelativePath("/a", "/a/b/x.txt")
The target is inside the start folder. Returns b/x.txt.
GetRelativePath("/a/b", "/a/b")
The same place. Returns ..
GetRelativePath("/a/b", "/a")
One level up. Returns ...
The comparison of folder names follows the file system rules of the system: case-insensitive on Windows, case-sensitive on Linux. On Windows, two paths on different drives have no relative path, so the second path is returned in its full form.
Library declarations
C#
public static string GetRelativePath(string relativeTo, string path);
Parameters
Parameter
Description
relativeTo
The starting point. It is treated as a folder, even if it has a dot in its name. It cannot be empty.
path
The destination, a file or a folder. It cannot be empty.
Return Value
Return Value
Description
string
The relative path from relativeTo to path. It is . when both are the same place. It is the full destination if there is no relative path, for example on another Windows drive.
C# Examples
Example 1 — Walk From One Folder to a File in Another
Build paths in the temp folder and print the relative route in three situations: sideways, down, and to the same place.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.GetTempPath();
string from = Path.Combine(root, "a", "b");
string target = Path.Combine(root, "a", "c", "d.txt");
Console.WriteLine(Path.GetRelativePath(from, target).Replace('\\', '/'));
Console.WriteLine(Path.GetRelativePath(Path.Combine(root, "a"), Path.Combine(from, "x.txt")).Replace('\\', '/'));
Console.WriteLine(Path.GetRelativePath(from, from));
}
}
Output
../c/d.txt
b/x.txt
.
How It Works
Sideways. From a/b the route to a/c/d.txt goes up one level (..) to a, then down into c. Replace keeps the separator the same on every system.
Downwards. The file x.txt is inside a/b, and the start folder is a, so only the down part b/x.txt is needed.
The same place. When both paths are equal, there is nothing to walk, and the answer is a single dot, which means "here".
Temp paths on purpose. All three paths are built from GetTempPath, which is full on every system. So the shared start of the paths never appears in the output.
Example 2 — Go There and Back: Relative Then Full
Make a relative path, join it to the start folder again, and check that it leads to the same target.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-relpath-demo");
string from = Path.Combine(root, "docs", "images");
string target = Path.Combine(root, "docs", "guide", "intro.md");
string rel = Path.GetRelativePath(from, target);
Console.WriteLine(rel.Replace('\\', '/'));
Console.WriteLine("Rooted: " + Path.IsPathRooted(rel));
string back = Path.GetFullPath(Path.Combine(from, rel));
Console.WriteLine("Back at target: " + (back == Path.GetFullPath(target)));
}
}
Output
../guide/intro.md
Rooted: False
Back at target: True
How It Works
Two sibling folders.images and guide are both inside docs. From images, the file is one level up and then into guide.
Not rooted. A relative path never starts at a root, so IsPathRooted is False. It is only meaningful together with its start folder.
Join it back.Combine(from, rel) gives a path with a .. inside it, and GetFullPath resolves that part.
Same place again. The resolved path equals the full form of the target, so the comparison prints True. This is how links between files in a folder tree are stored.
Example 3 — Relative Inputs Work Too
Both arguments can be short, relative paths. They are made full first, so the result is the same.
C#
using System;
using System.IO;
class Program {
static void Main() {
Console.WriteLine(Path.GetRelativePath("site/docs", "site/images/logo.png").Replace('\\', '/'));
Console.WriteLine(Path.GetRelativePath("site/docs/", "site/docs/guide/intro.md").Replace('\\', '/'));
Console.WriteLine(Path.GetRelativePath("site/docs", "site").Replace('\\', '/'));
Console.WriteLine(Path.GetRelativePath("a/b", "a/b/c/d/e.txt").Replace('\\', '/'));
}
}
Output
../images/logo.png
guide/intro.md
..
c/d/e.txt
How It Works
Both are completed first. The working directory is added in front of each argument. It is the same in both, so it cancels out and never shows in the result.
A sibling folder. From site/docs to site/images/logo.png is one step up, then down into images.
A trailing separator is fine."site/docs/" and "site/docs" mean the same folder, so the result is only the part below it.
Up and deeper. From site/docs to its parent site is just ... Going deeper, from a/b to a/b/c/d/e.txt gives c/d/e.txt, with no dots at all.
Frequently Asked Questions
It returns the relative path from one location to another, for example "../c/d.txt" from "/a/b" to "/a/c/d.txt". It uses text only and does not check the disk.
First the start folder (relativeTo), then the destination (path). Swapping them gives the reverse route, such as "../../b" instead of "c/d.txt".
A single dot. GetRelativePath("/a/b", "/a/b") is ".", which means "this folder".
On Windows, there is no relative path between two drives, so the method returns the second path as a full path. On Linux and macOS there is only one root, so this case does not occur.
Yes. Both arguments are converted with GetFullPath first, so the working directory is added to each. Both are then compared from the same start, and the result is the same as for full paths.