Directory.GetFiles 一次返回一个文件吗? (。网)

发布于 2024-08-21 23:58:43 字数 195 浏览 3 评论 0原文

我有一个文件夹,里面有太多文件,我想逐个浏览每个文件。问题是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

錯遇了你 2024-08-28 23:58:43

您无法在 .NET 3.5 中执行此操作,但在 .NET 4.0 中可以,根据这篇博文:(

DirectoryInfo directory = new DirectoryInfo(@"\\share\symbols");
IEnumerable<FileInfo> files = directory.EnumerateFiles();
foreach (var file in files) {
    Console.WriteLine("Name={0}, Length={1}", file.Name, file.Length);
}

同样有一个静态的 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:

DirectoryInfo directory = new DirectoryInfo(@"\\share\symbols");
IEnumerable<FileInfo> files = directory.EnumerateFiles();
foreach (var file in files) {
    Console.WriteLine("Name={0}, Length={1}", file.Name, file.Length);
}

(Likewise there's a static Directory.EnumerateFiles method.)

I don't know whether that API has been ported to Mono yet.

戏蝶舞 2024-08-28 23:58:43

查看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文