Path.Combine绝对与相对pathstring

我试图用Path.Combinejoin一个相对pathWindowspath。

但是, Path.Combine(@"C:\blah",@"..\bling")返回C:\blah\..\bling而不是C:\bling\

有没有人知道如何做到这一点,而不写我自己的相对pathparsing器(这不应该太难)?

Solutions Collecting From Web of "Path.Combine绝对与相对pathstring"

什么工程:

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

(result:absolutePath =“C:\ bling.txt”)

什么都行不通

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

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

在组合路径上调用Path.GetFullPath http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx

 > Path.GetFullPath(Path.Combine(@"C:\blah\",@"..\bling")) C:\bling 

(我同意Path.Combine本身应该这样做)

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

这将给你正是你所需要的(路径不必存在这个工作)

 DirectoryInfo di = new DirectoryInfo(@"C:\blah\..\bling"); string cleanPath = di.FullName; 

对于Windows通用应用程序Path.GetFullPath()不可用,您可以改为使用System.Uri类:

  Uri uri = new Uri(Path.Combine(@"C:\blah\",@"..\bling")); Console.WriteLine(uri.LocalPath);