How to exclude folders when using Directory.GetDirectories

前端 未结 2 1889
你的背包
你的背包 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:27

    the opposite of (A || B) is (!A && !B), so in your code it should be &&, not ||...

    0 讨论(0)
  • 2020-12-18 11:29

    You can do something like:

    //list your excluded dirs
    private List<string> _excludedDirectories= new List<string>() { "Admin", "Templates" };
    
    //method to check
    static bool isExcluded(List<string> 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));
    
    0 讨论(0)
提交回复
热议问题