Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9284643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:57:22+00:00 2026-06-18T18:57:22+00:00

It appears that there is no standard way to calculate LocalPath from a relative

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T18:57:24+00:00Added an answer on June 18, 2026 at 6:57 pm

    [editedit]
    Ok, after a bit of contemplation, I did come up with an alternative, but it may not be palatable to all:

    void Main()
    {
        var path1 = @"C:\Program Files\Internet Explorer\";
        var path2 = @"C:\temp\";
        var sb = new StringBuilder(1000);
        PathRelativePathTo(sb, path1, 0, path2, 0);
        sb.ToString().Dump();
    }
    
    /*
    BOOL PathRelativePathTo(
      _Out_  LPTSTR pszPath,
      _In_   LPCTSTR pszFrom,
      _In_   DWORD dwAttrFrom,
      _In_   LPCTSTR pszTo,
      _In_   DWORD dwAttrTo
    );
    */
    [DllImport("Shlwapi.dll")]
    [return:MarshalAs(UnmanagedType.Bool)]
    public static extern bool PathRelativePathTo(
        [Out] StringBuilder result,
        [In] string pathFrom,
        [In] int dwAttrFrom,
        [In] string pathTo,
        [In] int dwAttrTo);
    

    Ooh, just had an idea – does this get you (more or less) what you need?

    public string PathDiff(string path1, string path2)
    {
        var replace1 = path1.Replace(path2, string.Empty);
        var replace2 = path2.Replace(path1, string.Empty);
        return Path.IsPathRooted(replace1) ? replace2 : replace1;
    }
    

    Or possibly better:

    public string PathDiff(string path1, string path2)
    {
        return path1.Length > path2.Length ? 
            path1.Replace(path2, string.Empty) : 
            path2.Replace(path1, string.Empty);
    }
    

    (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:

    var path1 = @"C:\dev\src\release\Frontend\";
    var path2 = @"C:\dev\src\";
    
    var path1Uri = new Uri(path1);
    var path2Uri = new Uri(path2);
    
    var from1to2 = path1Uri.MakeRelativeUri(path2Uri).OriginalString;
    var from2to1 = path2Uri.MakeRelativeUri(path1Uri).OriginalString;
    
    Console.WriteLine("To go from {0} to {1}, you need to {2}", path1, path2, from1to2);
    Console.WriteLine("To go from {0} to {1}, you need to {2}", path2, path1, from2to1);
    

    Output:

    To go from C:\dev\src\release\Frontend\ to C:\dev\src\, you need to ../../
    To go from C:\dev\src\ to C:\dev\src\release\Frontend\, you need to release/Frontend/
    

    Now, as for the slash differences “\” vs “/”, if you wrap the end results in Path.GetFullPath, it will auto-resolve the differences:

    Console.WriteLine(Path.GetFullPath(Path.Combine(path1, from1to2)));
    Console.WriteLine(Path.GetFullPath(Path.Combine(path2, from2to1)));
    

    Output:

    C:\dev\src\
    C:\dev\src\release\Frontend\
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using standard mysql functions is there a way to write a query that will
It appears that ffmpeg now has a segmenter in it, or at least there
Is there an option to enable the drop down menu that appears when coding
Is there an event that fires in vb.net just before a contextMenuStrip appears when
Is there a standard way to close out an application cleanly while some WaitHandle
is there any STL algorithm or a standard way of finding how many occurences
I normally get Facebook JSON data that look like this (taken from an example):
I wish to skin my aplication and have noticed that there appears to be
I have an activity that appears as a dialog using the following custom theme:
Making websites that appear correctly in IE is a big problem. Is there any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.