It appears that there is no standard way to calculate LocalPath from a relative URI (this property is valid only for absolute URIs), to be used in conjunction with Path.Combine, for example to combine it with a file mask (*.ext). Problem is that MakeRelativeUri produces something similar to my%20folder/, instead of my folder\.
Here is a workaround that I found:
Module Module1
Sub Main()
Dim path1 As String = "C:\my folder\"
Dim path2 As String = "C:\"
MsgBox(GetPathDiff(path1, path2)) 'outputs "my folder\" (without quotes)
End Sub
Private Function GetPathDiff(path1 As String, path2 As String) As String
Dim uri1 As New Uri(path1)
Dim uri2 As New Uri(path2)
Dim uri3 As Uri = uri2.MakeRelativeUri(uri1)
Return Uri.UnescapeDataString(uri3.OriginalString).Replace("/", "\")
End Function
End Module
I find it a rather clumsy way, and there might be some hidden stones I did not yet stumble upon, i.e. this method being not 100% stable for different use cases.
Is there a better way to do it?
[editedit]
Ok, after a bit of contemplation, I did come up with an alternative, but it may not be palatable to all:
Ooh, just had an idea – does this get you (more or less) what you need?
Or possibly better:
(edit: derp, hit submit too soon):
There’s no built-in relative path helpers, unfortunately, but you’ve basically got it with what you’ve got, like so:
Output:
Now, as for the slash differences “\” vs “/”, if you wrap the end results in
Path.GetFullPath, it will auto-resolve the differences:Output: