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

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

问题描述

什么是检查目录1 2目录的子目录,反之亦然一种简单的方法?

What is an easy way to check if directory 1 is a subdirectory of directory 2 and vice versa?

我检查了路径和DirectoryInfo的helperclasses但没有发现系统 - 为此做好准备的功能。我认为这将是在那里的地方。

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.

推荐答案

您可以比较directory2到directory1中的 Parent属性在这两种情况下使用的DirectoryInfo时

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天全站免登陆