C# Program to Get the relative path from two absolute paths

In C#, finding the relative path between two absolute paths is a common task when working with file systems. We can achieve this using the Uri class and its MakeRelativeUri method. This technique is useful for creating relative links between files or navigating within applications.

An absolute path contains complete location information, such as C:\Program Files\Google\Chrome\filename.exe, while a relative path specifies location relative to the current directory, like Google\Chrome\filename.exe.

Syntax

Following is the syntax for creating URI instances and getting the relative path

Uri baseUri = new Uri(basePath);
Uri targetUri = new Uri(targetPath);
Uri relativeUri = baseUri.MakeRelativeUri(targetUri);

The MakeRelativeUri method returns a relative URI that navigates from the base URI to the target URI

string relativePath = baseUri.MakeRelativeUri(targetUri).ToString();

Understanding the Uri Class

The Uri class in the System namespace provides functionality for working with Uniform Resource Identifiers. Key constructors and methods include

  • new Uri(string uriString) Initializes a URI instance with the specified path

  • MakeRelativeUri(Uri uri) Returns the relative path between two URIs

  • ToString() Converts the URI to its string representation

Relative Path Calculation Base Path C:\App\Images Target Path C:\App\Docs\file.txt MakeRelativeUri Relative Path ../Docs/file.txt

Using MakeRelativeUri Method

Example 1 Basic Usage

using System;

class Program {
   static void Main(string[] args) {
      string basePath = @"C:\Projects\MyApp\Images";
      string targetPath = @"C:\Projects\MyApp\Documents\readme.txt";
      
      Uri baseUri = new Uri(basePath + @"");  // Add trailing slash for directory
      Uri targetUri = new Uri(targetPath);
      
      Uri relativeUri = baseUri.MakeRelativeUri(targetUri);
      string relativePath = relativeUri.ToString();
      
      Console.WriteLine("Base Path: " + basePath);
      Console.WriteLine("Target Path: " + targetPath);
      Console.WriteLine("Relative Path: " + relativePath);
   }
}

The output of the above code is

Base Path: C:\Projects\MyApp\Images
Target Path: C:\Projects\MyApp\Documents\readme.txt
Relative Path: ../Documents/readme.txt

Example 2 Web URLs

using System;

class Program {
   static void Main(string[] args) {
      string baseUrl = "https://www.example.com/products/electronics/";
      string targetUrl = "https://www.example.com/products/clothing/shirts.html";
      
      Uri baseUri = new Uri(baseUrl);
      Uri targetUri = new Uri(targetUrl);
      
      Uri relativeUri = baseUri.MakeRelativeUri(targetUri);
      
      Console.WriteLine("Base URL: " + baseUrl);
      Console.WriteLine("Target URL: " + targetUrl);
      Console.WriteLine("Relative URL: " + relativeUri.ToString());
   }
}

The output of the above code is

Base URL: https://www.example.com/products/electronics/
Target URL: https://www.example.com/products/clothing/shirts.html
Relative URL: ../clothing/shirts.html

Example 3 Different Drive Scenarios

using System;

class Program {
   static void Main(string[] args) {
      string sameDriveBase = @"C:\Folder1\SubFolder";
      string sameDriveTarget = @"C:\Folder2\file.txt";
      
      string differentDriveBase = @"C:\Folder1";
      string differentDriveTarget = @"D:\Folder2\file.txt";
      
      // Same drive example
      Uri baseUri1 = new Uri(sameDriveBase);
      Uri targetUri1 = new Uri(sameDriveTarget);
      string relative1 = baseUri1.MakeRelativeUri(targetUri1).ToString();
      
      Console.WriteLine("Same Drive Relative Path: " + relative1);
      
      // Different drives - returns absolute path
      Uri baseUri2 = new Uri(differentDriveBase);
      Uri targetUri2 = new Uri(differentDriveTarget);
      string relative2 = baseUri2.MakeRelativeUri(targetUri2).ToString();
      
      Console.WriteLine("Different Drive Result: " + relative2);
   }
}

The output of the above code is

Same Drive Relative Path: ../../Folder2/file.txt
Different Drive Result: file:///D:/Folder2/file.txt

Key Points

  • For directory paths, add a trailing slash (\ or /) to ensure correct relative path calculation

  • When paths are on different drives, MakeRelativeUri returns the absolute target URI

  • The method works with both file system paths and web URLs

  • The result uses forward slashes (/) regardless of the input path format

Conclusion

The Uri.MakeRelativeUri method provides an efficient way to calculate relative paths between two absolute paths in C#. It handles both file system paths and web URLs, making it useful for various navigation and linking scenarios in applications.

Updated on: 2026-03-17T07:04:36+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements