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

后端 未结 11 1602
北海茫月
北海茫月 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:23

    Here's a simpler way to do it using the Uri class:

    var parentUri = new Uri(parentPath);
    var childUri = new Uri(childPath);
    if (parentUri != childUri && parentUri.IsBaseOf(childUri))
    {
       //dowork
    }
    
    0 讨论(0)
  • 2020-12-31 04:24

    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 (d2.FullName.Contains(d1.FullName))
    {
         //dowork
    }
    

    If you need to check for mapped drives you could try

        static void Main(string[] args)
        {
            if (GetUNCPath(d2.FullName).ToLower().Contains(GetUNCPath(d1.FullName).ToLower()))
            {
            }
        }
    
        [DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        private static extern int WNetGetConnection(
            [MarshalAs(UnmanagedType.LPTStr)] string localName,
            [MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName, ref int length);
    
        private static string GetUNCPath(string originalPath)
        {
    
            StringBuilder sb = new StringBuilder(512);
            int size = sb.Capacity;
            // look for the {LETTER}: combination ...
            if (originalPath.Length > 2 && originalPath[1] == ':')
            {
                // don't use char.IsLetter here - as that can be misleading
                // the only valid drive letters are a-z && A-Z.
                char c = originalPath[0];
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
                {
                    int error = WNetGetConnection(originalPath.Substring(0, 2), sb, ref size);
                    if (error == 0)
                    {
                        DirectoryInfo dir = new DirectoryInfo(originalPath);
                        string path = Path.GetFullPath(originalPath).Substring(Path.GetPathRoot(originalPath).Length);
                        return Path.Combine(sb.ToString().TrimEnd(), path);
                    }
                }
            }
            return originalPath;
        }
    

    Code for mapped drive taken from http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/6f79f2b3-d092-431f-bc28-d15d93cf5d09

    0 讨论(0)
  • 2020-12-31 04:25

    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:

    var ancestor = new DirectoryPathAbsolute(ancestorPath);
    var child = new DirectoryPathAbsolute(childPath);
    
    var res = child.IsChildDirectoryOf(ancestor); //I don't think it actually checks for case-sensitive filesystems
    

    Otherwise, if you want to know whether a directory exists as a subdirectory in a path take a look on:

    Directory.EnumerateDirectories
    

    Came in .Net 4.0. Example:

    Does path contain a directory starting with Console:

    //* is a wildcard. If you remove it, it search for directories called "Console"
    var res = Directory.EnumerateDirectories(@path, "Console*", SearchOption.AllDirectories).Any();
    
    0 讨论(0)
  • 2020-12-31 04:29

    You can use Path.GetDirectoryName Method to get parent directory. It works for directories too.

    0 讨论(0)
  • 2020-12-31 04:31
    public static bool IsSubfolder(DirectoryInfo parentPath, DirectoryInfo childPath)
    {
    return parentPath.FullName.StartsWith(childPath.FullName+Path.DirectorySeparatorChar);
    }
    
    0 讨论(0)
提交回复
热议问题