[英]How to exclude sub directories based on array values
I need to get the full path from directories but use an array to exclude certain paths without entering the full path into the array.我需要从目录中获取完整路径,但使用数组来排除某些路径而不将完整路径输入到数组中。
I am getting the path of directories like this:我得到这样的目录路径:
List<string> dirs = di.GetDirectories("vault*", SearchOption.AllDirectories)
.Select(x => x.FullName).ToList();
The directory looks like this: I need to filter based on the parent after C.该目录如下所示:我需要根据 C 之后的父级进行过滤。
C:\A\vault
C:\B\vault
C:\C\vault
I have an array like this:我有一个这样的数组:
string[] exclude = new string[] {"A", "B"};
Doing something like below does not work because it will require I enter the full name of the path to exclude in the array, which can get nasty:执行以下操作不起作用,因为它需要我输入要在数组中排除的路径的全名,这可能会令人讨厌:
dirs.Except(exclude);
How can I do this better so that I can easily update the array without all the extraneous characters of longer paths?我怎样才能更好地做到这一点,以便我可以轻松地更新数组,而无需更长路径的所有无关字符? Example: Adding an additional path in the future to exclude.
示例:在将来添加要排除的其他路径。
I think this should work just fine:我认为这应该可以正常工作:
string[] exclude = new string[] { "A", "B" };
List<string> dirs =
di
.GetDirectories("vault*", SearchOption.AllDirectories)
.Where(x => !exclude.Contains(x.Name))
.Select(x => x.FullName)
.ToList();
I've tested it.我已经测试过了。 Note that the
Contains
being used is against exclude
- so this is checking whether the Name
exists within the array, it is not doing some form of substring search.请注意,使用的
Contains
是针对exclude
——因此这是检查Name
是否存在于数组中,而不是进行某种形式的子字符串搜索。
To be a little more robust it might be worth using this:为了更健壮,可能值得使用这个:
.Where(x => !exclude.Select(y => y.ToUpperInvariant()).Contains(x.Name.ToUpperInvariant()))
You have to changed SearchPattern
something like:您必须将
SearchPattern
更改为:
List<string> dirs = di.GetDirectories("C\\Vault*", SearchOption.AllDirectories)
.Select(x => x.FullName).ToList();
All A and B
are excluded already所有
A and B
已被排除
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.