Dominating
Dominating

Reputation: 2930

Directory.GetFiles does not work as expected

string[] list = Directory.GetFiles("c:\\", "One Two Three - User.xml")

This code does not returns me array, but I have three directories with this file. Any ideas how to make it work?

Upvotes: 0

Views: 1799

Answers (2)

Will A
Will A

Reputation: 24988

Check out the variation of this method that takes a SearchOption, here. It seems that you're after a recursive direction search, and the SearchOption enumeration allows you to specify this.

Upvotes: 2

Marco
Marco

Reputation: 57573

Directory.GetFiles does not traverse subdirs in this way... so only file on C:\ is returned!!
If you need to search this pattern in a dir and in its subdirs you have to scan (recursively) all subdirs and then current dir. In every step you add files to a global variable (string[] files).
I think this example can be useful...
Or you can use Directory.GetFiles(path, pattern, SearchOption.AllDirectories);

Upvotes: 1

Related Questions