How do I specify relative file path in

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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shahan AyyubSenior Software EngineerCommented:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HakanAuthor 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 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;

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

Open in new window

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

Open in new window

Shahan AyyubSenior Software EngineerCommented:
Seems like you are talking about regular string literal and verbatim string literal, please check this one:

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.
HakanAuthor Commented:
I didn't define string with "@" so i'm bit confused.
Shahan AyyubSenior Software EngineerCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.