10

Possible Duplicate:
How to get relative path from absolute path

I'm currently using the FolderBrowserDialog to return an absolute path. I would like to instead get the relative path with respect to the currently executing directory. Given that FolderBrowserDialog only returns an absolute path, is there a way to translate this to a relative path?

Community
  • 1
  • 1
amccormack
  • 13,207
  • 10
  • 38
  • 61
  • 2
    See http://stackoverflow.com/questions/275689/how-to-get-relative-path-from-absolute-path – SwDevMan81 Apr 19 '11 at 14:33
  • 4
    The [`PathRelativePathTo` function](http://msdn.microsoft.com/en-us/library/bb773740.aspx) is what you want. Ignore the nonsense about using the `Uri` class. You can find the [P/Invoke definition here](http://www.pinvoke.net/default.aspx/shlwapi.pathrelativepathto). – Cody Gray - on strike Apr 19 '11 at 14:36
  • I knew this question existed before, I remembered seeing it but couldn't find it. – amccormack Apr 19 '11 at 14:36
  • Appears you can use GetFullPath as well (http://stackoverflow.com/questions/670566/path-combine-absolute-with-relative-path-strings) – SwDevMan81 Apr 19 '11 at 14:38
  • @SwDevMan: `GetFullPath` returns the absolute path. The asker wants to translate an absolute path (as returned by `FolderBrowserDialog`) to a *relative* path. – Cody Gray - on strike Apr 19 '11 at 14:41

1 Answers1

6

You want to use Uri.MakeRelativeUri(uri). Get your current executing assembly, store it as a Uri. Create a second Uri from the directory from your Folder browser, and use MakeRelativeUri(). I asked a similar question touching on this, that goes in to details of properly decoding the Uri so that the path is a valid form for a Windows folder.

Removing %20 from URI Relative Path

Community
  • 1
  • 1
Stealth Rabbi
  • 10,156
  • 22
  • 100
  • 176
  • 1
    Why in the world would you do this for a *file system directory*? They call it the `Uri` class for a reason: it's designed for use with URIs. – Cody Gray - on strike Apr 19 '11 at 14:50
  • 1
    @Cory, that was the conclusion that was given for the question that was posted. I do not know of another way of doing it. If you have a more direct way of doing it with a file system directory, then you could, you know, PROVIDE an answer, as that's what this OP is asking, too. Have fun making a system call. – Stealth Rabbi Apr 19 '11 at 14:52
  • I didn't "PROVIDE an answer", because this question is a duplicate. Instead, I provided a "close as duplicate" vote. This answer is *much* less complete than the original that makes the same conclusion, and precisely the reason we discourage duplicate answers here. Just because you can't think of a better way doesn't mean it's the right way. – Cody Gray - on strike Apr 19 '11 at 15:10
  • 2
    Actually using Uri is argueably the most safe way to deal with paths of unknown origin that handle the most edge cases and security issues. The above answer is overly simplistic, however Uri based methods are used inside VS itself with path compares, rel and abs determination and manipulation and many other fucntions. Just because it says Uri it shouldnt be discounted. – Beeeaaar Feb 13 '13 at 00:53
  • Technically local paths still count as URI's, right? – Kyle Delaney Mar 28 '18 at 16:11