C#中的PathCanonicalize等效方法

9

在C#中,PathCanonicalize的等价物是什么?

用途:我需要猜测两个文件路径是否指向同一个文件(无需访问磁盘)。我的典型做法是通过一些过滤器(如MakeAbsolute和PathCanonicalize)处理它,然后进行不区分大小写的比较。

3个回答

12

快速且不太规范的:

过去,我曾经通过路径字符串创建FileInfo对象,然后使用FullName属性。这会删除所有的..\'s和.\'s。

当然你也可以使用交互操作:

 [DllImport("shlwapi", EntryPoint="PathCanonicalize")]
    private static extern bool PathCanonicalize(
        StringBuilder lpszDst,
        string lpszSrc
    );

6

3个解决方案:

最佳情况是您可以100%确定调用进程将完全访问文件系统。 注意:在生产环境中,权限可能会很棘手。

    public static string PathCombineAndCanonicalize1(string path1, string path2)
    {
        string combined = Path.Combine(path1, path2);
        combined = Path.GetFullPath(combined);
        return combined;
    }

但是,我们并不总是自由的。通常情况下,您需要在未经允许的情况下执行字符串算术运算。这里有一个本地调用可供使用。注意:会使用本地调用。

    public static string PathCombineAndCanonicalize2(string path1, string path2)
    {
        string combined = Path.Combine(path1, path2);
        StringBuilder sb = new StringBuilder(Math.Max(260, 2 * combined.Length));
        PathCanonicalize(sb, combined);
        return sb.ToString();
    }

    [DllImport("shlwapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool PathCanonicalize([Out] StringBuilder dst, string src);

第三种策略是欺骗CLR。Path.GetFullPath()在虚假路径上运行得很好,因此请确保您总是提供一个虚假的路径。您可以用虚假的UNC路径替换根路径,调用GetFullPath(),然后再将真实路径替换回来。注意:这可能需要在代码审查中进行艰难的推销。

    public static string PathCombineAndCanonicalize3(string path1, string path2)
    {
        string originalRoot = string.Empty;

        if (Path.IsPathRooted(path1))
        {
            originalRoot = Path.GetPathRoot(path1);
            path1 = path1.Substring(originalRoot.Length);
        }

        string fakeRoot = @"\\thiscantbe\real\";
        string combined = Path.Combine(fakeRoot, path1, path2);
        combined = Path.GetFullPath(combined);
        combined = combined.Substring(fakeRoot.Length);
        combined = Path.Combine(originalRoot, combined);
        return combined;
    }

1

Path.GetFullPath()不能使用相对路径。我一直在寻找适用于相对路径的解决方案。

我尝试了许多方法,但都没有起作用。@Paul的第三种策略在Linux上不起作用,\\并且在相对路径上存在错误,因为它会引入一个更多的文件夹,在结果中会失去一个..

这是一种适用于相对路径和绝对路径的解决方案。它适用于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));
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接