views:

2855

answers:

3

I'm trying to join a windows path with a relative path using Path.Combine

However, Path.Combine(@"C:\blah\",@"..\bling") return "C:\blah..\bling" instead of "C:\bling\"

Does anyone know how to accomplish this without writing my own relative path resolver (which shouldn't be too hard)?

I know it seems stupid to ask for code to simple things (like I have with other questions), but I;d feel stupider if I didn't find out if there was a "proper" framework way to do things first.

+7  A: 

I am annoyed by this as well. The framework is lousy IMO when it comes to simple canonicalization.

Check out Path.GetFullPath (which touches the file system!) and possibly System.Uri.

Brian
+4  A: 

Path.GetFullPath(@"c:\windows\temp\..\system32")?

shahkalpesh
+6  A: 

What Works:

string relativePath = "..\\bling.txt";
string baseDirectory = "C:\\blah\\";
string absolutePath = Path.GetFullPath(baseDirectory + relativePath);

(result: absolutePath="C:\blah\bling.txt")

What doesn't work

string relativePath = "..\\bling.txt";
Uri baseAbsoluteUri = new Uri("C:\\blah\\");
string absolutePath = new Uri(baseAbsoluteUri, relativePath).AbsolutePath;

(result: absolutePath="C:/blah/bling.txt")

vanslly
You should be using the Path class, not Url...
Frank Schwieterman
Yes, that is what I am insinuating with the post
vanslly