1. Herfried K. Wagner’s VB.Any
  2. .NET
  3. Häufig gestellte Fragen

Ermitteln einer relativen aus einer absoluten Pfadangabe

Ermitteln einer relativen aus einer absoluten Pfadangabe
<URL:https://dotnet.currifex.org/dotnet/faqs/makepathrelative/de/>
----------------------------------------------------------------------------

Getting a relative out of an absolute path

The managed way:

\\\
Dim u1 As New Uri("C:\WINDOWS\SYSTEM32\TEST\FOO")
Dim u2 As New Uri("C:\WINDOWS")
Dim s1 As String = u1.MakeRelative(u2)
Dim s2 As String = u2.MakeRelative(u1)
///

The unmanaged way:

\\\
Private Declare Auto Function PathRelativePathTo Lib "shlwapi.dll" ( _
    ByVal pszPath As String, _
    ByVal pszFrom As String, _
    ByVal dwAttrFrom As Int32, _
    ByVal pszTo As String, _
    ByVal dwAttrTo As Int32 _
) As Int32

Private Const FILE_ATTRIBUTE_DIRECTORY As Int32 = &H10
Private Const MAX_PATH As Int32 = 260

Private Sub Test()
    Dim s1 As String = "C:\WINDOWS\SYSTEM32\TEST\FOO"
    Dim s2 As String = "C:\WINDOWS"
    Dim x As String = Space(MAX_PATH)
    PathRelativePathTo( _
        x, _
        s1, _
        FILE_ATTRIBUTE_DIRECTORY, _
        s2, _
        FILE_ATTRIBUTE_DIRECTORY _
    )
    MsgBox(x.Substring(1, x.IndexOf(ControlChars.NullChar) - 1))
End Sub
///