Csharp/C#教程:C#中的PathCanonicalize等价物分享


C#中的PathCanonicalize等价物

与C#中的PathCanonicalize相同的是什么?

使用:我需要好好猜测两个文件路径是否引用同一个文件(没有磁盘访问)。 我的典型方法是通过一些filter(如MakeAbsolute和PathCanonicalize)抛出它,然后进行不区分大小写的比较。

又快又脏:

在过去,我从路径字符串创建了一个FileInfo对象,然后使用了FullName属性。 这将删除所有.. 和。’。

你当然可以互操作:

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

3解决方案:

最佳情况,您100%确定调用进程将具有对文件系统的完全访问权限。 CAVEAT:对生产箱的许cocoa能很棘手

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

但是,我们并不总是自由的。 通常你需要在未经许可的情况下进行字符串算术。 有一个本地的呼吁。 CAVEAT:转向原生呼叫

  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(),然后重新交换真实的.CAVEAT:这可能需要在代码审查中出售硬盘

上述就是C#学习教程:C#中的PathCanonicalize等价物分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

  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 = @"\thiscantbereal"; string combined = Path.Combine(fakeRoot, path1, path2); combined = Path.GetFullPath(combined); combined = combined.Substring(fakeRoot.Length); combined = Path.Combine(originalRoot, combined); return combined; } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1006335.html

(0)
上一篇 2021年12月29日
下一篇 2021年12月29日

精彩推荐