Click here to Skip to main content
15,912,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

Please see my code below to select all files under the selected folder

List<string> search;
search = Directory.GetFiles(@folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories).ToList();


This works well with small folders.

Whenever I choose a drive D: or E: it just keeps working. Is there any better way to faster handle this ?

Thanks !
Posted
Comments
BillWoodruff 29-Oct-14 2:29am    
What is the reason to (recursively) get ALL the Files in a drive, including all files in all directories in that drive ?
tvks 1-Nov-14 22:01pm    
Forcibly my requirement is to get all files in a selected path. When the user chooses a drive, my app goes for a toss (hanging) !!

I don't know if any of the file enumeration techniques shown here http://msdn.microsoft.com/en-us/library/dd997370(v=vs.110).aspx[^] are faster - obviously they are worth a shot - also, again obvious, I'd try to keep the search path as narrow/refined as possible to try and avoid the 'AllDirectories'
 
Share this answer
 
Comments
tvks 1-Nov-14 22:02pm    
Hi Garth ! From my test I hardly find any difference when my C drive is choosen. It does hang.
I suggest you really think deeply about whether or not you actually need to recursively get the filepaths of all files, in all directories, in a drive ! I'd guess you don't need to do that, and that you will want to filter the results by some criteria, like file-extension, etc.

First, as Garth points out to you in his reply, you definitely should use .NET 4.0 and later's 'EnumerateFiles method rather than 'GetFiles [^]:
"The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient."
You can find excellent examples, and techniques, for creating efficient file searches using various options, and Linq, with EnumerateFiles here: [^].
 
Share this answer
 
Comments
tvks 1-Nov-14 22:06pm    
Thanks for trying to help.
1. My requirement (specs) says an option to any folder/drive and I need to list all the files.
2. As said above either have any difference if for example C drive is choosen

Also, if there is an exception, I assume it hangs. Like when Recycle Bin folder comes up, I assume there is an access problem which might be the reason. How can I skip such files and keep running ??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900