'DirectoryInfo.GetFiles slow when using SearchOption.AllDirectories

I am searching a moderate number (~500) of folders for a large number (~200,000) of files from a .NET application.

I hoped to use DirectoryInfo.GetFiles, passing in SearchOption.AllDirectories. However this approach seems to be a lot slower than writing my own code to iterate through the directories and do GetFiles just passing in a searchPattern.

Related MSDN info:

  • GetFiles(String)
    Returns a file list from the current directory matching the given searchPattern.
  • GetFiles(String, SearchOption)
    Returns a file list from the current directory matching the given searchPattern and using a value to determine whether to search subdirectories.

Has anyone had a similar experience to this?



Solution 1:[1]

The approach Anton mentioned using FindFirstFile() and related native methods has been implemented as of .NET 4 via DirectoryInfo.EnumerateFiles() so no more need for P/Invoke for this!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1