How do I specify relative file path in VB.net

Hakan
Hakan used Ask the Experts™
on
I'm getting some file info in my application and i'd like write them if have relative directory relationship in my application.

For example my directory: C:/files

If it's found file in;
C:/files/test1.pdf    'Output: ../test1.pdf
C:/files/190501/test2.pdf 'Output: ../190501/test2.pdf
C:/archive/180501/test3.pdf 'Output: C:/archive/180501/test3.pdf

I search on net and Path.GetRelativePath would fit my desire but it's not supported at .net 4.8 so Any help would be grateful.

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Software Engineer
Commented:

Author

Commented:
Hi thanks for the source. It really helped me. I changed last section of code because it gives me "\\" instead of "\". Is it wrong or another thing you know?
Base Code:
public static string GetRelativePath(string fullPath, string basePath ) 
{
    // Require trailing backslash for path
    if (!basePath.EndsWith("\\"))
        basePath += "\\";

    Uri baseUri = new Uri(basePath);
    Uri fullUri = new Uri(fullPath);

    Uri relativeUri = baseUri.MakeRelativeUri(fullUri);

    // Uri's use forward slashes so convert back to backward slashes
    return relativeUri.ToString().Replace("/", "\\");

}

Open in new window


VB.net Converted Code:

    Public Shared Function GetRelativePath(ByVal fullPath As String, ByVal basePath As String) As String
        If Not basePath.EndsWith("\\") Then
            basePath += "\\"
        End If

        Dim baseUri As Uri = New Uri(basePath)
        Dim fullUri As Uri = New Uri(fullPath)

        Dim relativeUri As Uri = baseUri.MakeRelativeUri(fullUri)

        Return relativeUri.ToString().Replace("/", "\")
    End Function

Open in new window


i changed the last section;

From
 relativeUri.ToString().Replace("/", "\\")

Open in new window

To
relativeUri.ToString().Replace("/", "\")

Open in new window

Shahan AyyubSenior Software Engineer

Commented:
Seems like you are talking about regular string literal and verbatim string literal, please check this one:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim

Point #2 seems to be this case. If you define string with '@' then '\' is fine otherwise you should use '\\'. In case of directory or file path, path separator (\) is in between the string so it was replaced as '\\' ('\' character escaped with '\', similarly in \", quotation(") escaped with '\') to keep it in string as-is treat it as a regular string literal.

Author

Commented:
I didn't define string with "@" so i'm bit confused.
Shahan AyyubSenior Software Engineer

Commented:
Ahh, I didn't see you are looking for VB.Net snippet. This (@) is for C# only. VB.Net works without '@' and '\\'. Thats why you need to change '\\' with '\'. If you will run this code in C# then you need these things.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial