开发者

Have Directory.GetFiles return one file at a time? (.NET)

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.开发者_如何学Go)

Many thanks.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜