共用方式為


Path.GetRelativePath(String, String) 方法

定義

傳回某個路徑到另一個路徑的相對路徑。

public:
 static System::String ^ GetRelativePath(System::String ^ relativeTo, System::String ^ path);
public static string GetRelativePath (string relativeTo, string path);
static member GetRelativePath : string * string -> string
Public Shared Function GetRelativePath (relativeTo As String, path As String) As String

參數

relativeTo
String

應與結果相對的來源路徑。 此路徑一律視為目錄。

path
String

目的地路徑。

傳回

如果路徑不共用相同的根目錄,則為相對路徑或 path

例外狀況

relativeTopathnull

relativeTopath 實際上是空的。

範例

下列程式碼將示範如何呼叫 GetRelativePath 方法。

// "C:/Program Files/Microsoft" relative to "C:/Program Files/Common Files" is "../Microsoft"
Console.WriteLine(Path.GetRelativePath("C:/Program Files/Common Files", "C:/Program Files/Microsoft"));
// "C:/Program Files/Microsoft" relative to "C:/Program Files/" is "Microsoft"
Console.WriteLine(Path.GetRelativePath("C:/Program Files/", "C:/Program Files/Microsoft"));

// This code produces output similar to the following:
//
// ../Microsoft
// Microsoft
' "C:/Program Files/Microsoft" relative to "C:/Program Files/Common Files" is "../Microsoft"
Console.WriteLine(Path.GetRelativePath("C:/Program Files/Common Files", "C:/Program Files/Microsoft"))
' "C:/Program Files/Microsoft" relative to "C:/Program Files/" is "Microsoft"
Console.WriteLine(Path.GetRelativePath("C:/Program Files/", "C:/Program Files/Microsoft"))

' This code produces output similar to the following:
'
' ../Microsoft
' Microsoft

備註

在計算差異之前, GetFullPath 會先呼叫 方法來解析路徑。 方法會針對適用于 Windows 和 MacOs 的目前平臺 (StringComparison.OrdinalIgnoreCase 使用預設檔案路徑比較, StringComparison.Ordinal 適用于 Linux。

適用於

另請參閱