我尝试使用来搜索文件系统
Directory.EnumerateDirectories
比方说,我想在Windows中搜索整个C:\,但是我想排除某些路径(例如C:\ asdf,C:\ lorem \ ipsum)或文件夹名称(例如folder1,folder2,... )。我想创建一个从字符串列表中过滤出路径和文件夹的语句。这意味着:
List<string> exclude = new List<string>{@"C:\asdf", @"C:\lorem\ipsum", "folder2"};
如何从上述目录搜索中排除这些路径和文件夹名称?
答案 0 :(得分:3)
List<string> exclude = new List<string> { @"C:\asdf", @"C:\lorem\ipsum", "folder2" };
Func<string, string, bool> containsCaseInsensitivePredicate = (s, p) => s.IndexOf(p, StringComparison.OrdinalIgnoreCase) != -1;
Func<string, bool> notInExcludeListPredicate = (s) => !exclude.Any(ex => containsCaseInsensitivePredicate(ex, s));
IEnumerable<string> directories = Directory.EnumerateDirectories(@"C:\").Where(notInExcludeListPredicate);