Path.Combine absolute with relative path strings

CVertex picture CVertex · Mar 22, 2009 · Viewed 97.1k times · Source

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

However, Path.Combine(@"C:\blah",@"..\bling") returns 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)?

Answer

Llyle picture Llyle · Aug 19, 2009

What Works:

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

(result: absolutePath="C:\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")