11

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.

billpg
  • 3,195
  • 3
  • 30
  • 57

2 Answers2

8

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • just a question, why not use: var files = directory.EnumerateFiles(); ? – Filip Ekberg Feb 19 '10 at 10:21
  • 1
    @Filip: I don't know about Jon's reasons, but I would use the type name for clarity since it is not obvious from the method name what type it returns. – Fredrik Mörk Feb 19 '10 at 10:24
  • @Fredrik, isn't it clear enough that it is going to return an enumeratable file info list? The Method-name is really self-explaining. The same goes for DirectoryInfo, i would rather use var directory =... I know that there is a difference at compile time though. – Filip Ekberg Feb 19 '10 at 10:27
  • @Fredrik: That example is copied directly from the referenced blog post. – Jon Skeet Feb 19 '10 at 10:36
  • Thanks. I was hoping for something that would work in .NET 2, but if it's not there, it's not there. (That's the second rule of Tuatology Club.) – billpg Feb 19 '10 at 15:05
1

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.

Konstantin Spirin
  • 20,609
  • 15
  • 72
  • 90
  • Thanks, I may end up using that, but falling back to Directory.GetFiles if it throws an interop exception, indicating that I'm probably not on win32. – billpg Feb 26 '10 at 11:56
  • 1
    You can use `Environment.OSVersion` to determine if you are running under Windows or not and find some other code that will work fast on the operating system you are targeting. – Konstantin Spirin Feb 28 '10 at 14:06