FrankVDB
FrankVDB

Reputation: 235

C# - SearchOption.AllDirectories but not in certain folders

I'd like to use this Directory.GetFiles(source, file, SearchOption.AllDirectories))

But do not search in specific folders.

How can I manage that? Thank you!

Upvotes: 0

Views: 339

Answers (1)

Rufus L
Rufus L

Reputation: 37020

Using GetFiles with SearchOption.AllDirectories will search all directories. If you aren't worried about the performance hit of searching all directories, and just want to filter the results, you can do something like this:

var pathsToAvoid = new List<string> { @"c:\public\temp\", @"c:\public\media\" };

var files = Directory.GetFiles(@"c:\public\", "temp.txt", SearchOption.AllDirectories)
    .Where(filePath => !pathsToAvoid.Any(path =>
    filePath.StartsWith(path, StringComparison.OrdinalIgnoreCase)));

Otherwise, you would need to manually search the directories you care about one at a time.

Upvotes: 2

Related Questions