Search code examples
c#.netdirectorygetfiles

Using Directory.EnumerateFiles()


I am iterating through a particular directory in my code using Directory.EnumerateFiles(). I only need the file name for each file present in the directory but number of files can be huge. As per MSDN and various other sources, we see it mentioned that "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.", here.

So, I did a bit of experiment on my machine.

var files = Directory.EnumerateFiles(path);
foreach(var file in files)
{
}

I had a debugger before starting the iteration, i.e. at foreach. I see the files (returned enumerable of strings) contains all the entries in my directory. Why this? Is this expected?

On some other sources I see that we need to have a separate task which does the iteration and simultaneously in another processing thread, the enumerable can be iterated, example. I was looking for something like: an enumerator by default fetching let's say first 200 files. Successive calls to MoveNext() i.e. when 201'th item is accessed, the next batch of files are fetched.


Solution

  • The variable files here is an IEnumerable, lazily evaluated. If you hover over files in the debugger and click 'Results view' then the full evaluation will take place (just as if you'd called, say, ToArray()). Otherwise the files will only be fetched as you need them (i.e. one at a time by the foreach loop).

    So when you say:

    "I see the files (returned enumerable of strings) contains all the entries in my directory"

    I think you are mistaken.