How to check if directory 1 is a subdirectory of dir2 and vice versa

后端 未结 11 1672
北海茫月
北海茫月 2020-12-31 04:07

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-r

11条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-31 04:17

    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");
    }
    

提交回复
热议问题