Given full path, check if path is subdirectory of some other path, or otherwise

前端 未结 9 1374
难免孤独
难免孤独 2020-12-03 13:22

I have 2 strings - dir1 and dir2, and I need to check if one is sub-directory for other. I tried to go with Contains method:

dir1.contains(dir2);
         


        
9条回答
  •  忘掉有多难
    2020-12-03 13:49

    In my case the path and possible subpath do not contains '..' and never end in '\':

    private static bool IsSubpathOf(string path, string subpath)
    {
        return (subpath.Equals(path, StringComparison.OrdinalIgnoreCase) ||
                subpath.StartsWith(path + @"\", StringComparison.OrdinalIgnoreCase));
    }
    

提交回复
热议问题