Directory.GetFiles挂起驱动器选择 [英] Directory.GetFiles Hangs on drive selection

查看:76
本文介绍了Directory.GetFiles挂起驱动器选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好



请参阅下面的代码选择所选文件夹下的所有文件



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();





适用于小文件夹。



每当我选择驱动器D:或E:它只是继续工作。有没有更好的方法来更快地处理这个?



谢谢!



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 !

推荐答案

我不知道如果此处显示的任何文件枚举技术 http://msdn.microsoft。 com / en-us / library / dd997370(v = vs.110).aspx [ ^ ]更快 - 显然它们值得一试 - 同样,显而易见的是,我会尽量保持搜索路径的缩小/尽可能精炼以避免'AllDirectories'
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'


我建​​议你真的深入思考你是否真的需要递归获取所有文件的文件路径,在所有目录中,在驱动器中!我猜你不需要这样做,并且你想要按照某些标准过滤结果,比如文件扩展等。



First正如Garth在回复中指出的那样,你肯定应该使用.NET 4.0及更高版本的'EnumerateFiles方法而不是'GetFiles [ ^ ]:
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."

你可以找到很好的例子和技术,用于使用各种选项创建有效的文件搜索,Linq,使用EnumerateFiles:[ ^ ]。

You can find excellent examples, and techniques, for creating efficient file searches using various options, and Linq, with EnumerateFiles here: [^].


这篇关于Directory.GetFiles挂起驱动器选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆