Path.Combine absolute with relative path strings
Solution 1
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")
Solution 2
Call Path.GetFullPath on the combined path http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx
> Path.GetFullPath(Path.Combine(@"C:\blah\",@"..\bling"))
C:\bling
(I agree Path.Combine ought to do this by itself)
Solution 3
Path.GetFullPath(@"c:\windows\temp\..\system32")?
Solution 4
For windows universal apps Path.GetFullPath()
is not available, you can use the System.Uri
class instead:
Uri uri = new Uri(Path.Combine(@"C:\blah\",@"..\bling"));
Console.WriteLine(uri.LocalPath);
Solution 5
This will give you exactly what you need (path does NOT have to exist for this to work)
DirectoryInfo di = new DirectoryInfo(@"C:\blah\..\bling");
string cleanPath = di.FullName;
Related videos on Youtube
CVertex
"There's a risk I'll OD on imagination like my friend Chauncey" - Wonder Showzen.
Updated on July 08, 2022Comments
-
CVertex almost 2 years
I'm trying to join a Windows path with a relative path using
Path.Combine
.However,
Path.Combine(@"C:\blah",@"..\bling")
returnsC:\blah\..\bling
instead ofC:\bling\
.Does anyone know how to accomplish this without writing my own relative path resolver (which shouldn't be too hard)?
-
CVertex about 15 yearsWe're getting different answers here.. I don't think it's a duplicate
-
Greg Dean about 15 yearsit's duplicate, although i think Path.GetFullName is a better solution.
-
CVertex about 15 yearsYou just contradicted yourself. But thanks for the alternate answer.
-
Julien Bérubé almost 10 yearspossible duplicate of Path.Combine and the dot notation
-
-
Llyle over 14 yearsYes, that is what I am insinuating with the post
-
Nelson Rothermel almost 11 yearsJust make sure baseDirectory has the trailing \\, otherwise you end up with
C:\\blah..\\bling.txt
and that doesn't work. In that case you can manually add them to the string or doPath.GetFullPath(Path.Combine(baseDirectory, relativePath))
-
cod3monk3y over 9 yearsNote: should result in
c:\windows\system32
-
cod3monk3y over 9 yearsShouldn't the result of your What Works section be
C:\bling.txt
? -
Paul Williams over 9 yearsBoth Path.GetFullPath() and DirectoryInfo.FullName will work on a fictitious path. The problem is when the file actually exists, the executing process needs FileIOPermission - true for both APIs. (see MSDN)
-
F-H over 6 yearsWhy does the URI-based method not work? According to this answer, the result is valid (and it seems to be recognized on Windows, as well).
-
derekantrican about 4 yearsNote that this only works if the first path is an absolute path. It doesn't work for
Path.GetFullPath(Path.Combine(@"..\..\blah",@"\bling"))