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:\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.
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:
List<string> dirs = di.GetDirectories("C\\Vault*", SearchOption.AllDirectories)
.Select(x => x.FullName).ToList();
All A and B
are excluded already
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.