Path.Combine绝对值与相对路径字符串

CVe*_*tex 85 .net c# windows filesystems path

我正在尝试使用相对路径加入Windows路径Path.Combine.

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

有没有人知道如何在不编写我自己的相对路径解析器的情况下完成此任务(这不应该太难)?

Lly*_*yle 59

什么有效:

string relativePath = "..\\bling.txt";
string baseDirectory = "C:\\blah\\";
string absolutePath = Path.GetFullPath(baseDirectory + relativePath);
Run Code Online (Sandbox Code Playgroud)

(结果:absolutePath ="C:\ bling.txt")

什么行不通

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

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

  • 是的,这就是我对帖子的暗示 (8认同)
  • 只要确保baseDirectory有尾随\\,否则你最终得到`C:\\ blah .. \\ bling.txt`,但这不起作用.在这种情况下,您可以手动将它们添加到字符串或执行`Path.GetFullPath(Path.Combine(baseDirectory,relativePath)) (7认同)
  • 你的__What Works__部分的结果不应该是`C:\ bling.txt`吗? (5认同)

Col*_*nic 30

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

> Path.GetFullPath(Path.Combine(@"C:\blah\",@"..\bling"))
C:\bling
Run Code Online (Sandbox Code Playgroud)

(我同意Path.Combine应该自己做)

  • 请注意,这仅在第一个路径是绝对路径时才有效。它不适用于 `Path.GetFullPath(Path.Combine(@"..\..\blah",@"\bling"))` (2认同)

sha*_*esh 15


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

  • 注意:应该导致`c:\ windows\system32` (2认同)

thu*_*eys 5

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

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


U. *_*lle 5

Path.GetFullPath()不适用于相对路径。

这是适用于相对路径和绝对路径的解决方案。它适用于 Linux + Windows,并按..预期保留文本开头的内容(静止时它们将被标准化)。该解决方案仍然依赖于Path.GetFullPath通过一个小的解决方法来进行修复。

这是一个扩展方法,所以使用它就像text.Canonicalize()

/// <summary>
///     Fixes "../.." etc
/// </summary>
public static string Canonicalize(this string path)
{
    if (path.IsAbsolutePath())
        return Path.GetFullPath(path);
    var fakeRoot = Environment.CurrentDirectory; // Gives us a cross platform full path
    var combined = Path.Combine(fakeRoot, path);
    combined = Path.GetFullPath(combined);
    return combined.RelativeTo(fakeRoot);
}
private static bool IsAbsolutePath(this string path)
{
    if (path == null) throw new ArgumentNullException(nameof(path));
    return
        Path.IsPathRooted(path)
        && !Path.GetPathRoot(path).Equals(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal)
        && !Path.GetPathRoot(path).Equals(Path.AltDirectorySeparatorChar.ToString(), StringComparison.Ordinal);
}
private static string RelativeTo(this string filespec, string folder)
{
    var pathUri = new Uri(filespec);
    // Folders must end in a slash
    if (!folder.EndsWith(Path.DirectorySeparatorChar.ToString())) folder += Path.DirectorySeparatorChar;
    var folderUri = new Uri(folder);
    return Uri.UnescapeDataString(folderUri.MakeRelativeUri(pathUri).ToString()
        .Replace('/', Path.DirectorySeparatorChar));
}
Run Code Online (Sandbox Code Playgroud)