C#程式:獲取兩個絕對路徑的相對路徑


介紹

讓我們嘗試理解如何使用C#程式從兩個絕對路徑中獲取相對路徑。我們將學習如何使用URI(統一資源識別符號)類和MakeRelativeUri方法。

首先,我們將瞭解絕對路徑和相對路徑的區別。絕對路徑包含定位系統上檔案或目錄所需的所有資訊。例如,絕對路徑為C:\Program Files\Google Chrome\filename.exe。

相對路徑告訴我們檔案路徑相對於使用者正在工作的當前目錄。考慮上面提到的類似示例,如果主可執行檔案位於C:\Program Files中,則filename.exe的相對路徑為Google Chrome\filename.exe。

我們將使用MakeRelativeUri方法生成輸出。在詳細解釋上述方法之前,我們應該瞭解此方法所在的類和名稱空間。我們將首先了解System.IO名稱空間,然後瞭解URI類。最後,我們將深入研究MakeRelativeUri方法、其程式碼和演算法。

System.IO名稱空間

System.IO名稱空間是C#中許多類和方法工作原理的基礎。它提供了各種類,幫助我們執行輸入和輸出操作。藉助它,我們可以讀取和寫入檔案和各種目錄。它包含的一些類如下。

  • File  我們可以使用此類建立、複製、刪除和移動檔案。

  • Directory  它提供建立、刪除和對目錄執行各種其他操作的方法。

  • String  它用於表示字元序列。

  • Math  它提供在C#程式中執行數學運算的方法。

  • Path  它提供處理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://tutorialspoint.tw”);

建立URI例項後,您可以使用它來訪問一些方法和屬性。我們將在下面訪問的一種方法是MakeRelativeUri方法。

MakeRelativeUri方法

這是C#中URI類下的一個方法。它用於從兩個絕對路徑查詢相對路徑。它是System.IO路徑類下的內建方法,它接受兩個絕對路徑作為輸入,並返回兩者之間的相對路徑。在現實世界中,當您需要在兩個檔案之間建立連結或要在應用程式中導航時,它非常有用。

從兩個絕對路徑建立相對路徑的語法如下:

Uri baseUri = new Uri(" https://tutorialspoint.tw/");
Uri absoluteUri = new Uri("https://tutorialspoint.tw/code1");
Uri relativeUri = baseUri.MakeRelativeUri(absoluteUri);
Console.WriteLine(relativeUri); 

演算法

下面的演算法將詳細解釋MakeRelativeUri方法的概念以及如何從兩個絕對路徑中找出相對路徑。我們將逐步瞭解如何使用此方法。

步驟1 −  輸入引數包含兩個路徑:絕對路徑和相對路徑。

步驟2 − 我們將把兩個路徑都作為字串形式的輸入儲存。

步驟3  我們將使用以下程式碼為兩者建立Uri例項:

Uri baseUri = new Uri(basePath);

步驟4  現在,我們將呼叫所需的方法來生成兩者之間的相對路徑,即MakeRelativeUri方法。

步驟5  現在,我們將結果相對路徑儲存在輸出字串中。

步驟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方法的瞭解。

更新於:2023年4月21日

5000+ 次瀏覽

開啟你的職業生涯

完成課程並獲得認證

開始學習
廣告