如何检查目录1是否为dir2的子目录,反之亦然 [英] How to check if directory 1 is a subdirectory of dir2 and vice versa

查看:149
本文介绍了如何检查目录1是否为dir2的子目录,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查目录1是目录2的子目录,反之亦然吗?



我检查了Path和DirectoryInfo辅助类,但没有发现系统 - 准备好这个功能。我认为这会在某个地方。



你们有什么想法找到这个吗?



我试着自己写一个支票,但是比起我预期的更复杂一些。

解决方案

你可以将directory2与directory1的 父属财产 DirectoryInfo d2 = new DirectoryInfo(@C:\Program Files\MyApp\Images);

if(d2.Parent.FullName == d1.FullName)
{
Console.WriteLine(Sub directory);
}


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.

解决方案

You can compare directory2 to directory1's Parent property when using a DirectoryInfo in both cases.

DirectoryInfo d1 = new DirectoryInfo(@"C:\Program Files\MyApp");
DirectoryInfo d2 = new DirectoryInfo(@"C:\Program Files\MyApp\Images");

if(d2.Parent.FullName == d1.FullName)
{
    Console.WriteLine ("Sub directory");
}

这篇关于如何检查目录1是否为dir2的子目录,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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