如何比较 C# 中的(目录)路径? [英] How can I compare (directory) paths in C#?

查看:25
本文介绍了如何比较 C# 中的(目录)路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有两个 DirectoryInfo 对象,我如何比较它们的语义相等性?例如,以下路径都应该被认为等于 C: emp:

If I have two DirectoryInfo objects, how can I compare them for semantic equality? For example, the following paths should all be considered equal to C: emp:

  • C: emp
  • C: emp
  • C: emp.
  • C: empx.... emp.

以下可能等于也可能不等于C: emp:

The following may or may not be equal to C: emp:

  • emp 如果当前工作目录在驱动器 C:
  • temp 如果当前工作目录是 C:
  • C: emp.
  • C: emp...
  • emp if the current working directory is on drive C:
  • temp if the current working directory is C:
  • C: emp.
  • C: emp...

如果考虑当前工作目录很重要,我可以自己弄清楚,所以这不是那么重要.尾随点在窗口中被剥离,所以这些路径应该是相等的 - 但它们在 unix 中没有被剥离,所以在单声道下我期待其他结果.

If it's important to consider the current working directory, I can figure that out myself, so that's not that important. Trailing dots are stripped in windows, so those paths really should be equal - but they aren't stripped in unix, so under mono I'd expect other results.

区分大小写是可选的.路径可能存在也可能不存在,用户可能有也可能没有路径权限 - 我更喜欢一种不需要任何我的快速健壮的方法/O(所以没有权限检查),但如果有内置的东西,我会对任何足够好"的东西感到满意;还有...

Case sensitivity is optional. The paths may or may not exist, and the user may or may not have permissions to the path - I'd prefer a fast robust method that doesn't require any I/O (so no permission checking), but if there's something built-in I'd be happy with anything "good enough" too...

我意识到,如果没有 I/O,就不可能确定某个中间存储层是否恰好将相同的存储映射到了相同的文件(即使有 I/O,当事情变得足够混乱时,这可能是不可能的).但是,无论底层文件系统如何,至少应该可以肯定地识别等效的路径,即在给定类型的所有可能的文件系统上必须解析为相同文件(如果存在)的路径.这有时有用的原因是(A)因为我当然想在进行 I/O 之前先检查一下,(B)I/O 有时会触发有问题的副作用,以及(C)各种其他软件组件有时会破坏提供的路径,并且能够以对等效路径的最常见转换不敏感的方式进行比较是有帮助的,最后(D)准备部署,事先进行一些健全性检查很有用,但这些发生在要部署之前 -系统甚至可以访问.

I realize that without I/O it's not possible to determine whether some intermediate storage layer happens to have mapped the same storage to the same file (and even with I/O, when things get messy enough it's likely impossible). However, it should be possible to at least positively identify paths that are equivalent, regardless of the underlying filesystem, i.e. paths that necessarily would resolve to the same file (if it exists) on all possible file-systems of a given type. The reason this is sometimes useful is (A) because I certainly want to check this first, before doing I/O, (B) I/O sometimes triggers problematic side-effects, and (C) various other software components sometimes mangle paths provided, and it's helpful to be able to compare in a way that's insensitive to most common transformations of equivalent paths, and finally (D) to prepare deployments it's useful to do some sanity checks beforehand, but those occur before the to-be-deployed-on system is even accessible.

推荐答案

来自这个答案,这个方法可以处理一个少数边缘情况:

From this answer, this method can handle a few edge cases:

public static string NormalizePath(string path)
{
    return Path.GetFullPath(new Uri(path).LocalPath)
               .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
               .ToUpperInvariant();
}

原始答案中有更多详细信息.像这样称呼它:

More details in the original answer. Call it like:

bool pathsEqual = NormalizePath(path1) == NormalizePath(path2);

应该适用于文件和目录路径.

Should work for both file and directory paths.

这篇关于如何比较 C# 中的(目录)路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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