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

前端 未结 9 1353
难免孤独
难免孤独 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 14:11

    public static bool IsSubpathOf(string rootPath, string subpath)
    {
        if (string.IsNullOrEmpty(rootPath))
            throw new ArgumentNullException("rootPath");
        if (string.IsNullOrEmpty(subpath))
            throw new ArgumentNulLException("subpath");
        Contract.EndContractBlock();
    
        return subath.StartsWith(rootPath, StringComparison.OrdinalIgnoreCase);
    }
    

提交回复
热议问题