8

When searching for files in a directory tree ( Folder and all sub-folders), what is the effective difference between doing this:

Directory.GetFiles(root, "*", SearchOption.AllDirectories);

and doing your own recursive search using

Directory.GetFiles(root) and Directory.GetDirectories(root)

What are the pros and cons for using each method, which method is suited for which use case? Thanks.

Sachin Joseph
  • 18,928
  • 4
  • 42
  • 62
Nitkov
  • 468
  • 6
  • 18

1 Answers1

3

The main reason you might want to 'roll your own' recursion in this case could be that you want to be able to set up custom progress updating / notification to users during a long file search.

This isn't possible if you hand everything over to the framework method from the start.

Baldrick
  • 11,712
  • 2
  • 31
  • 35
  • 1
    Another scenario where you'd use your own recursion is where the process may not have access to some sub-directories (as described in this [answer](https://stackoverflow.com/a/929418/945456)). – Jeff B Jun 12 '17 at 16:20
  • @JeffBridgman: Good point. Basically, if you want fine grained control over the search process! The default provided is good for most purposes, but it's good to have more control for some applications. – Baldrick Jun 12 '17 at 16:46