How to exclude folders when using Directory.GetDirectories

前端 未结 2 1890
你的背包
你的背包 2020-12-18 11:21

I want to return a list of all the subdirectories in the \'SomeFolder\' directory excluding the \'Admin\' and \'Templates\' directories.

I have th

2条回答
  •  一整个雨季
    2020-12-18 11:29

    You can do something like:

    //list your excluded dirs
    private List _excludedDirectories= new List() { "Admin", "Templates" };
    
    //method to check
    static bool isExcluded(List exludedDirList, string target)
    {
        return exludedDirList.Any(d => new DirectoryInfo(target).Name.Equals(d));
    }
    
    //then use this
    var filteredDirs = Directory.GetDirectories(path).Where(d => !isExcluded(_excludedDirectories, d));
    

提交回复
热议问题