Home >Backend Development >C#.Net Tutorial >C# program gets relative path from two absolute paths

C# program gets relative path from two absolute paths

WBOY
WBOYforward
2023-09-17 13:25:021621browse

C# 程序从两个绝对路径获取相对路径

Introduction

Let us try to understand the C# program to get relative path from two absolute paths. We will use URI (Uniform Resource Identifier) ​​class and MakeRelativeUri method to understand.

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

The relative path tells us the path of the file relative to the current directory the user is working in. Consider the similar example mentioned above, if the main executable is located in C:\Program Files, the relative path to filename.exe is Google Chrome\filename.exe.

We will use the MakeRelativeUri method to generate the output. Before starting the detailed explanation of the above method, we should understand that class is the namespace which is the root of the method. We will first understand the System.IO namespace and then we will understand the URI class. Finally, we'll delve into the MakeRelativeUri method, its code, and algorithm.

System.IO Namespace

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

  • File We can create, copy, delete and move files through this class.

  • Directory It provides methods to create, delete, and perform various other operations on directories.

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

  • Math It provides methods to perform mathematical operations in C# programs.

  • PathIt provides methods for processing files in C# programs. We can get the file name with or without extension from the absolute path. With the help of this method, we will achieve the purpose of this article, which is to get 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 file system. It is a class that provides several properties and methods for handling URIs. Some explanations below -

  • new Uri(string string1) Initializes a new instance of the Uri class using the specified URI string.

  • new Uri(string string1, string string2) Initializes a new instance of the Uri class using the specified URI string and uriKind.

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

The syntax for creating a URI instance is as follows -

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

After you create a URI instance, you can use it to access certain methods and properties. One such method that we will visit below is the MakeRelativeUri method.

MakeRelativeUri方法

这是 C# 中 URI 类下的一个方法。它用于从两个绝对路径中查找相对路径。它是 System.IO 路径类下的内置方法,以两个绝对路径作为输入,并返回两者之间的相对路径。在现实世界中,当您需要在两个文件之间创建链接或想要在应用程序中导航时,它会很有帮助。

从两个绝对路径创建相对路径的语法如下所示−

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); 

算法

下面的算法将详细介绍MakeRelativeUri方法的概念,以及我们如何从两个绝对路径中找出相对路径。我们将逐步了解使用该方法的方法。

第 1 步 −  在输入参数中,我们有绝对路径和相对路径两种路径。

第二步 − 我们将把两个路径都以字符串的形式作为输入存储。

第 3 步我们将使用以下代码为它们创建 Uri 实例 -

Uri baseUri = new Uri(basePath);

第 4 步  现在我们将调用所需的方法来生成两个之间的相对路径,即 MakeRelativeUri 方法。

第五步  现在我们将把结果相对路径存储在输出字符串中。

第 6 步最后,我们将打印结果输出。

示例

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.
   }
}

输出

NewFolder/../Documents\File.doc

时间复杂度

在上面的代码中,我们借助了 Uri 类。 Uri 类实例已由输入参数创建。类实例的时间复杂度为 O(1)。同样,在下面的代码中,调用的 MakeRelativeUri 方法也需要 O(1) 时间复杂度。总体而言,代码的时间复杂度为O(1)。

结论

在本文中,我们彻底讨论了 Uri 类的使用,并了解了它的一些方法。我们已经严格理解了这样一种方法,那就是MakeRelativeUri方法。

我们希望本文有助于增强您对 Uri 类和 MakeRelativeUri 方法的了解。

The above is the detailed content of C# program gets relative path from two absolute paths. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete