This earlier SO question talks about how to get all the files in a directory tree that match one of several extensions.
eg. Extract all the files in C: \ and all the subdirectories corresponding to * .log, * .txt, * .dat.
The accepted answer was as follows:
var files = Directory.GetFiles("C:\\path", "*.*", SearchOption.AllDirectories)
.Where(s => s.EndsWith(".mp3") || s.EndsWith(".jpg"));
It seems to me completely ineffective. If you searched in a directory tree containing thousands of files (it uses SearchOption.AllDirectories), each individual file in the specified directory tree is loaded into memory, and only then inconsistencies are removed. (Reminds me of the "paging" offered by ASP.NET datagrids.)
Unfortunately, the standard System.IO.DirectoryInfo.GetFiles method accepts only one filter at a time.
It may be just my lack of Linq knowledge, is it really inefficient in the way I mention?
Secondly, is there a more efficient way to do this with or without Linq (without resorting to multiple GetFiles calls)?
source
share