如何检查目录 1 是否是 dir2 的子目录,反之亦然
有什么简单的方法可以检查目录 1 是否是目录 2 的子目录,反之亦然?
我检查了 Path 和 DirectoryInfo 帮助程序类,但发现没有系统就绪的函数。我以为它会在那里的某个地方。
你们知道在哪里可以找到这个吗?
我尝试自己写一张支票,但它比我开始时预期的要复杂。
What is an easy way to check if directory 1 is a subdirectory of directory 2 and vice versa?
I checked the Path and DirectoryInfo helperclasses but found no system-ready function for this. I thought it would be in there somewhere.
Do you guys have an idea where to find this?
I tried writing a check myself, but it's more complicated than I had anticipated when I started.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
为了回答问题的第一部分:“dir1 是 dir2 的子目录吗?”,此代码应该有效:
URI
(至少在 Windows 上,在 Mono/Linux 上可能有所不同) ) 不区分大小写。如果区分大小写很重要,请改用Uri
上的Compare
方法。In response to the first part of the question: "Is dir1 a sub-directory of dir2?", this code should work:
The
URI
s (on Windows at least, might be different on Mono/Linux) are case-insensitive. If case sensitivity is important, use theCompare
method onUri
instead.这是使用 Uri 类的更简单的方法:
Here's a simpler way to do it using the Uri class:
请参阅此处的原始答案: https://stackoverflow.com/a/31941159/134761
的混合\
和/
文件夹分隔符..\
c:\foobar
不是子路径) ofc:\foo
)代码:
测试用例 (NUnit):
更新 2015-08-18:修复部分文件夹名称匹配的错误。添加测试用例。
2016 年 1 月 29 日更新:原始问题链接 https://stackoverflow.com/a/31941159/134761
See original answer here: https://stackoverflow.com/a/31941159/134761
\
and/
folder delimiters..\
in pathc:\foobar
not a subpath ofc:\foo
)Code:
Test cases (NUnit):
Update 2015-08-18: Fix bug matching on partial folder names. Add test cases.
Update 2016-01-29: Link to original question https://stackoverflow.com/a/31941159/134761
DirectoryInfo 有一个属性 Parent,它也是 DirectoryInfo 类型。您可以使用它来确定您的目录是否是父目录的子目录。
DirectoryInfo has a property Parent which is also a DirectoryInfo type. You can use that to to determine if your directory is a subdirectory of a parent directory.
如果第二个目录(d2)是 d1 的子文件夹,则其全名将包含第一个目录(d1)的全名。
这假设您正在使用有效的目录
如果您需要检查映射驱动器,您可以尝试
从http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/6f79f2b3-d092 -431f-bc28-d15d93cf5d09
The second directories(d2) full name will contain the full name of the first directory(d1) if it is a sub-folder of d1.
This assumes that you are using valid directories
If you need to check for mapped drives you could try
Code for mapped drive taken from http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/6f79f2b3-d092-431f-bc28-d15d93cf5d09
如果您有两个路径,请查看以下内容:
在 C# 中标准化目录名称
http://filedirectorypath.codeplex.com/ (我不知道它的质量)
并使用这个:
否则,如果您想知道某个目录是否作为路径中的子目录存在,请查看:
来自
.Net 4.0
。示例:path
是否包含以Console
开头的目录:If you have two path then look at this:
Normalize directory names in C#
http://filedirectorypath.codeplex.com/ (I don't know the quality of it)
And use this:
Otherwise, if you want to know whether a directory exists as a subdirectory in a path take a look on:
Came in
.Net 4.0
. Example:Does
path
contain a directory starting withConsole
:您可以使用 Path.GetDirectoryName 方法 来获取父目录。它也适用于目录。
You can use Path.GetDirectoryName Method to get parent directory. It works for directories too.
在angularsen的答案中编写的优秀测试用例的帮助下,我在.NET Core 3.1 for Windows上编写了以下更简单的扩展方法:
With help from the great test cases written in angularsen's answer, I wrote the following simpler extension method on .NET Core 3.1 for Windows:
这就是我得到的,在首先验证两个目录路径字符串是某种东西并且采用路径格式后我知道一些:
shouldnotbechilddirpath.ToUpper().StartsWith(maybeparentdirpath.ToUpper())
确保如果您可能在区分大小写的文件系统中工作,请取出 ToUppers() 。
this is what I got, after first verifying that the two directory path strings are something and in a path format I know something about:
shouldnotbechilddirpath.ToUpper().StartsWith(maybeparentdirpath.ToUpper())
Be sure to take out the ToUppers() if you are maybe working in a case sensitive file system.
您可以将directory2 与directory1 的
父属性<进行比较/code>
在这两种情况下使用 DirectoryInfo 时。
You can compare directory2 to directory1's
Parent property
when using a DirectoryInfo in both cases.