如何检查 2 个 DirectoryInfo 对象是否指向同一个目录? [英] How to check whether 2 DirectoryInfo objects are pointing to the same directory?

查看:24
本文介绍了如何检查 2 个 DirectoryInfo 对象是否指向同一个目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个 DirectoryInfo 对象,想检查它们是否指向同一个目录.除了比较他们的全名,还有其他更好的方法来做到这一点吗?请忽略链接的大小写.

I have 2 DirectoryInfo objects and want to check if they are pointing to the same directory. Besides comparing their Fullname, are there any other better ways to do this? Please disregard the case of links.

这是我所拥有的.

DirectoryInfo di1 = new DirectoryInfo(@"c:	emp");
DirectoryInfo di2 = new DirectoryInfo(@"C:TEMP");

if (di1.FullName.ToUpperInvariant() == di2.FullName.ToUpperInvariant())
{ // they are the same
   ...   
}

谢谢.

推荐答案

在 Linux 下,您可以比较两个文件的 INode 编号是否相同.但是在 Windows 下没有这样的概念,至少我知道不是.您需要使用 p/invoke 来解析链接(如果有).

Under Linux you could compare the INode numbers of the two files wheather they are identical. But under Windows there is no such concept, at least not that I know off. You would need to use p/invoke to resolve the links if any.

比较字符串是你能做的最好的事情.请注意,使用 String.Compare(str1, str2, StringComparison.OrdinalIgnoreCase) 比您的 ToUpperInvariant() 方法更快,因为它不会在堆上分配新字符串,并且不会遭受使用语言固有的问题文本比较算法比较文件路径.

Comparing strings is the best you can do. Note that using String.Compare(str1, str2, StringComparison.OrdinalIgnoreCase) is faster than your approach of ToUpperInvariant() as it doesn't allocate new strings on the heap and won't suffer problems inherent in using a linguistic text comparison algorithm to compare file paths.

这篇关于如何检查 2 个 DirectoryInfo 对象是否指向同一个目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆