Path.GetRelativePath(String, String) Methode

Definitie

Retourneert een relatief pad van het ene pad naar het andere.

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

Parameters

relativeTo
String

Het bronpad waar het resultaat ten opzichte van moet zijn. Dit pad wordt altijd beschouwd als een map.

path
String

Het doelpad.

Retouren

Het relatieve pad of path als de paden niet dezelfde hoofdmap delen.

Uitzonderingen

relativeTo of path is null.

relativeTo of path is effectief leeg.

Voorbeelden

De volgende code laat zien hoe u de GetRelativePath methode aanroept.

// "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

Opmerkingen

Paden worden opgelost door de GetFullPath methode aan te roepen voordat het verschil wordt berekend. De methode gebruikt de standaardbestandspadvergelijking voor het huidige platform (StringComparison.OrdinalIgnoreCase voor Windows en macOS, StringComparison.Ordinal voor Linux).

Van toepassing op

Zie ook