Directory.GetFiles 一次返回一个文件吗? (。网)
我有一个文件夹,里面有太多文件,我想逐个浏览每个文件。问题是 Directory.GetFiles 返回一个完整的数组,这需要很长时间。
我宁愿有一个指向文件夹的对象,然后调用一个函数来返回文件夹中的下一个文件。请问.NET 有这样的类吗?
(我宁愿避免 win32 互操作,因为我也计划在 Mono 上使用它。)
非常感谢。
I have a folder with far too many files in, and I want to go through each file one by one. The problem is that Directory.GetFiles returns a completed array, and this takes too long.
I would rather have an object I would point to a folder, then call a function that returns me the next file in the folder. Does .NET have a class like this please?
(I'd prefer to avoid win32 interops, as I plan to use this on Mono as well.)
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法在 .NET 3.5 中执行此操作,但在 .NET 4.0 中可以,根据这篇博文:(
同样有一个静态的
Directory.EnumerateFiles
方法。)我不知道该 API 是否已移植到 Mono。
You can't do this in .NET 3.5, but you can in .NET 4.0, as per this blog post:
(Likewise there's a static
Directory.EnumerateFiles
method.)I don't know whether that API has been ported to Mono yet.
查看 CodeProject 网站上的 FastDirectoryEnumerator 项目。
它完全满足您的需要,甚至更重要的是,我能够在包含大量文件的慢速网络共享上成功使用它,并且性能非常好。
缺点 - 它使用互操作,因此可能无法移植到 Mono。
Take a look at FastDirectoryEnumerator project on CodeProject web site.
It does exactly what you need and even more, I was able to successfully use it on a slow network share with lots of files and performance was just great.
Drawback - it uses interop so it may not be portable to Mono.