How to EnumerateFiles with all subdirectories with C# DirectoryInfo?
Asked Answered
B

1

5

I found this code that gets an array of files out of a DirectoryInfo :

FileInfo[] fileInfoArray = di.EnumerateFiles().Where(f => extensions.Contains(f.Extension.ToLower())).ToArray();

But it only searches the direct children of the path of DirectoryInfo. i.e., it does not include grandchildren.

I guess I need to add SearchOption.AllDirectories parameter to somewhere, but where?

I tried :

di.EnumerateFiles(SearchOption.AllDirectories).Where(f => extensions.Contains(f.Extension.ToLower())).ToArray();

But it yields an error.

So how do I search with a pattern, including all subdirectories ?

Thanks for any help !

Bucher answered 27/11, 2013 at 22:2 Comment(0)
T
14

Look at the overloads of DirectoryInfo.EnumerateFiles - there's no overload taking just a SearchOption, but you can give a string and a SearchOption:

var files = di.EnumerateFiles("*", SearchOption.AllDirectories)
              .Where(f => extensions.Contains(f.Extension.ToLower()))
              .ToArray();
Traceetracer answered 27/11, 2013 at 22:4 Comment(7)
you are so fast we have no chance for scoresManchineel
I didn't know about "*". Thanks :) Can accept in 8 minutes.Bucher
This is exactly what I needed, thanks. Still not accepted BTW :PBlasius
Don't use this because iterating through this (IENUMERABLE, e.g. with foreach) is extremely dangerous in the Windows file system because on possible access restrictions. On not few directories looping through "files" in such code will throw exceptions. Read msdn.microsoft.com/en-us/library/dd997370.aspx : "To enumerate a collection of FileInfo objects in all directories". You need to nest EnumerateFiles and EnumerateDirectories as one solution because EnumerateFiles seems to be only "safe" on the first directory level.Sydelle
@Philm: It's not as black and white as that. Certainly if you're going from the root directory, or over system directories etc, this could throw exceptions - but in many cases where the caller knows more about the directory structure they're working with, there's no need for the extra complexity. (I'd also consider "extremely dangerous" to be pretty strong hyperbole - I'd say something is "extremely dangerous" if it can do significant harm to your system. An exception being thrown isn't going to break your system unless you've got other, more serious issues in your code.)Traceetracer
@Jon Skeet: Of course you are right- If you are very sure about the context and the directories where it is used, you don't have to catch anything ;-) I'd consider such code to be used quite likely as a snippet, at least easily copied and taken for different contexts. IMHO the recommendation can be only: Put this error-prone task in a well-tested function of your own library- and part of this would be of course full error-handling.Sydelle
@MC10, he said he could accept it in 8 minutes, not that he would accept it in 8 minutes. Or at all. :)Booty

© 2022 - 2024 — McMap. All rights reserved.