简介
让我们尝试理解C#程序,从两个绝对路径获取相对路径。我们将使用URI(统一资源标识符)类和MakeRelativeUri方法来理解。
我们首先要了解绝对路径和相对路径之间的区别。绝对路径包括定位系统上的文件或目录所需的所有信息。绝对路径的一个示例是 C:Program FilesGoogle Chromefilename.exe。
相对路径告诉我们文件相对于用户正在工作的当前目录的路径。考虑上面提到的类似示例,如果主可执行文件位于 C:Program Files 中,则 filename.exe 的相对路径为 Google Chromefilename.exe。
我们将使用MakeRelativeUri方法来生成输出。在开始对上述方法进行详细解释之前,我们应该了解类是命名空间,它是该方法的根源。我们首先要了解System.IO命名空间,然后我们会了解URI类。最后,我们将深入研究MakeRelativeUri方法、它的代码和算法。
System.IO命名空间
System.IO 命名空间是 C# 中多个类和方法工作背后的原因。它提供了各种类来帮助我们执行输入和输出操作。借助它,我们可以读写文件和各种目录。其下的一些类如下。
文件 − 我们可以通过这个类来创建、复制、删除和移动文件。
目录− 它提供了在目录上创建、删除和执行各种其他操作的方法。
字符串− 它用于表示字符序列。
Math − 它提供了在C#程序中执行数学运算的方法。
路径−它提供了在 C# 程序中处理文件的方法。我们可以从绝对路径中获取带或不带扩展名的文件名。借助此方法,我们将实现本文的目的,即从两个绝对路径获取相对路径,
URI 类
C# 中的 Uri(统一资源标识符)类是 System.IO 命名空间中的内置类,用于标识 Internet 或文件系统中的资源。它是一个提供多个用于处理 URI 的属性和方法的类。下面解释了一些 -
new Uri(string string1) − 使用指定的URI字符串初始化Uri类的新实例。
new Uri(string string1, string string2) − 使用指定的URI字符串和uriKind初始化Uri类的新实例。
MakeRelativeUri − 它用于从两个绝对路径获取相对路径。
创建 URI 实例的语法如下 -
Uri uri = new Uri(“https://www.tutorialspoint.com”);
创建 URI 实例后,您可以使用它来访问某些方法和属性。我们将在下面访问的一种此类方法是 MakeRelativeUri 方法。
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:DriveAppImages";
// 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:DriveAppDocumentsFile.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/../DocumentsFile.doc
时间复杂度
在上面的代码中,我们借助了 Uri 类。 Uri 类实例已由输入参数创建。类实例的时间复杂度为 O(1)。同样,在下面的代码中,调用的 MakeRelativeUri 方法也需要 O(1) 时间复杂度。总体而言,代码的时间复杂度为O(1)。
结论
在本文中,我们彻底讨论了 Uri 类的使用,并了解了它的一些方法。我们已经严格理解了这样一种方法,那就是MakeRelativeUri方法。
我们希望本文有助于增强您对 Uri 类和 MakeRelativeUri 方法的了解。