Accepted answer

I'm afraid you can't.

Unlike GetFiles, EnumerateFiles doesn't load the whole list of results in memory: you can enumerate results before the end of the search.

However, it'll always ask for permissions on the directory currently being searched before returning any result for that specific directory. This means it'll throw the exception before the Where clause is checked.

As a side note, not only Recycle Bin will throw an exception, but all other directories the user that run your process doesn't have access to.

You should go for the solution you found that enumerates files and directories separately (with a try/catch).

Related Articles