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


Introduction

Let us try to understand the C# program to get the relative path from two absolute paths. We will understand using the URI( Uniform resource identifier) class and the MakeRelativeUri method.

We will first understand the difference between absolute and relative paths. The absolute path includes all information required to locate a file or directory on a system. An example of an absolute path is C:\Program Files\Google Chrome\filename.exe.

The relative path tells us the file's path with respect to the current directory on which the user is working. Considering the similar example mentioned above if the main executable is in C:\Program Files, the relative path to filename.exe is Google Chrome\filename.exe.

We will make use of the MakeRelativeUri method to generate the output. Before beginning a detailed explanation of the above-mentioned method, we should understand the classes are namespaces which is the root of this method. We will first understand the System.IO namespace and then we will understand the URI class. Finally, we will deep dive into the MakeRelativeUri method, its code, and its algorithm.

System.IO namespace

The System.IO namespace is the reason behind the working of multiple classes and methods in C#. It provides various classes that help us perform input and output operations. With its help of it we can read and write files and various directories. Some of the classes that come under it are as follows.

  • File  We can create, copy, delete move files with the help of this class.

  • Directory  It provides methods for creating, deleting, and performing various other operations on directories.

  • String  It is used to represent a sequence of characters.

  • Math  It provides methods for performing mathematical operations in the C# program.

  • Path  It provides methods for handling files in a C# Program. We can get the name of the file with and without an extension from the absolute path. With the help of this method we shall be fulfilling the motive of this article which is getting the relative path from two absolute paths,

URI Class

The Uri (Uniform Resource Identifier) class in C# is a built-in class in the System.IO namespace that is used to identify resources on the Internet or in a file system. It is a class that provides multiple properties and methods for working with URIs. Some are explained below −

  • new Uri(string string1)  It initializes a new instance of the Uri class with the specified URI string.

  • new Uri(string string1, string string2)  It initializes a new instance of the Uri class with the specified URI string and uriKind.

  • MakeRelativeUri  It is used to get the relative paths from two absolute paths.

The syntax to create a URI instance is as follows −

Uri uri = new Uri(“https://www.tutorialspoint.com”);

After you have created a URI instance, you can use it to access some methods and properties. One such method we will be accessing down below is the MakeRelativeUri method.

MakeRelativeUri Method

This is a method that comes under the URI class in C#. It is used to find the relative paths from two absolute paths. It is a built-in method under System.IO path class that takes two absolute paths as an input and it returns the relative path between the two. In the real world, it is helpful when you need to create links between two files or when you want to navigate within your application.

The syntax to create a relative path from two absolute paths is given below −

Uri baseUri = new Uri(" http://www.tutorialspoint.com/");
Uri absoluteUri = new Uri("http://www.tutorialspoint.com/code1");
Uri relativeUri = baseUri.MakeRelativeUri(absoluteUri);
Console.WriteLine(relativeUri); 

Algorithm

The Algorithm below will give an elaborate understanding of the concept of the MakeRelativeUri method and how we can find out the relative paths from two absolute paths. We will understand step-by-step methods to use this method.

Step 1 −  In the input parameters, we have two paths absolute paths and relative paths.

Step 2 − We will have both paths stored in the form of strings as inputs.

Step 3  We will create Uri Instances for both of them by using the following code −

Uri baseUri = new Uri(basePath);

Step 4  Now we will call the required method to generate the relative path between two i.e. MakeRelativeUri method.

Step 5  Now we will store the resultant relative path in the output string.

Step 6  Finally, We will print the resultant output.

Example

using System;
using System.IO;
using System.Text;
class FileName {
   static void Main(string[] args) {
      String path1= @"C:\Drive\App\Images"; 
      
      // here we have stored the first absolute path in the form of a string.
      
      // we have taken the name of the string as path1.
      string path2=  @"C:\Drive\App\Documents\File.doc";
      
      // we have stored the second absolute path in the form of a string.
      
      //we have taken the name path2.
      Uri uri1= new Uri(path1);
      
      //now we have created a Uri instance of path1.
      
      // we have named it uri1.
      Uri uri2= new Uri(path2);
      
      // we have created another Uri instance of path2.
      
      // we have named it uri2.
      string relativePath =uri1.MakeRelativeUri(uri2).ToString();
      
      //here we have called the MakeRelativeUri method in order to generate the output.
      
      // the output generated here stores the relative path from two absolute paths.
      
      //we have stored it in the form of a string.
      
      //the string has been named as relativePath
      string ans= "NewFolder/" + relativePath;
      Console.WriteLine(ans); // the answer is printed finally.
   }
}

Output

NewFolder/../Documents\File.doc

Time Complexity

In the code above we are taking the help of the Uri class. The Uri class instances have been created of input parameters. The class instances are taking O(1) time complexity. Similarly down below in the code, the MakeRelativeUri method called is also taking O(1) time complexity. Overall, the time complexity of the code is O(1).

Conclusion

In this article, we have thoroughly discussed the use of Uri class and have also got to know about some of the methods which come under it. We have rigorously understood one such method which is MakeRelativeUri method.

We hope that this article has been helpful in enhancing your knowledge of Uri class and MakeRelativeUri methods.

Updated on: 21-Apr-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements