Path.Combine absolute with relative path lines.

I am 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 do this without writing down their own relative path recognizer (which should not be too complicated)?

+70
c # windows filesystems path
Mar 22 '09 at 4:50
source share
5 answers

What works:

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

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

What does not work

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

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

+46
Aug 19 '09 at 11:33
source share
— -

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 that Path.Combine should do it myself)

+21
May 20 '13 at 9:44
source share
 Path.GetFullPath(@"c:\windows\temp\..\system32")?
Path.GetFullPath(@"c:\windows\temp\..\system32")? 
+14
Mar 22 '09 at 5:09
source share

This will give you exactly what you need (a path should not exist for this)

 DirectoryInfo di = new DirectoryInfo(@"C:\blah\..\bling"); string cleanPath = di.FullName; 
+3
Jul 17 '13 at 10:31 on
source share

For windows, the generic Path.GetFullPath() applications Path.GetFullPath() not available, you can use the System.Uri class instead:

  Uri uri = new Uri(Path.Combine(@"C:\blah\",@"..\bling")); Console.WriteLine(uri.LocalPath); 
+2
May 31 '16 at 20:13
source share



All Articles