问题
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 !
回答1:
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();
来源:https://stackoverflow.com/questions/20253936/how-to-enumeratefiles-with-all-subdirectories-with-c-sharp-directoryinfo