176

What is the difference between Directory.EnumerateFiles vs GetFiles?

Obviously one returns an array and the other return Enumerable.

Anything else?

Frank Shearar
  • 17,012
  • 8
  • 67
  • 94
DarthVader
  • 52,984
  • 76
  • 209
  • 300

3 Answers3

201

From the docs:

The EnumerateFiles and GetFiles methods differ as follows: 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. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

So basically, EnumerateFiles returns an IEnumerable which can be lazily evaluated somewhat, whereas GetFiles returns a string[] which has to be fully populated before it can return.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
  • 7
    Lazy evaluation may not come for completely free - for example if you will be putting it back to an array in the end (I saw that!). Lazy is efficient when "yagni"-all: you are not going to need all elements, and just stop iterating after a few. – Tomasz Gandor Apr 03 '13 at 15:16
  • 11
    Lazy enumeration is also interesting if you're going to enumerate a so large collection that building the whole collection first in memory is too heavy. You can just process the items as they come and forget them. – Stéphane Gourichon Jan 07 '16 at 18:49
  • 1
    @TomaszGandor: Or when there's no need to store all filenames, e.g. when just renaming files. – Sebastian Mach Jan 04 '18 at 09:21
  • 2
    I looked into the sources and found that both methods use internal `FileSystemEnumerableFactory.CreateFileInfoIterator()`. But `EnumerateFiles()` returns this iterator directly while `GetFiles()` builds extra `List` from it and calls `ToArray()`. So, if you care of speed it makes sense to use `EnumerateFiles()` and to deal with the iterator yourself. – dmitry1100 Apr 23 '20 at 08:52
  • 4
    Another good example is when you actually don't care about any of the files, and you just want to check if the folder has any files or empty. In that case you can simply use `Directory.EnumerateFiles(path).Any()`, which will return immediately after the first file and almost as fast as when the folder is empty. – Racil Hilan May 09 '21 at 02:57
38

EnumerateFiles returns IEnumerable<string> and that implies deferred execution. It is only available in .net 4 and up.

As the File system is notoriously slow (especially for large folders) the deferred execution can be a real bonus for sequential processing. Depending on lots of other factors.

H H
  • 263,252
  • 30
  • 330
  • 514
20

When using EnumerateFiles, all speed is lost if you are then using .Last. This makes sense of course, because to get to the last file, it will need to enumerate all files, then grab the last one.

However, using .First or .FirstOrDefault becomes very fast, because it simply grabs the first item and moves on.

Skotte
  • 329
  • 2
  • 11
  • 6
    This very old question asked what differences there were apart from the `Array` vs `Enumerable` aspect. Your answer is generic to all such situations but does not answer the question asked. – Ashigore Jun 11 '14 at 17:48
  • 2
    The point is, EnumerateFiles lets you start accessing data quicker, in some cases. – Skotte Mar 25 '15 at 04:47
  • 19
    The point is, this is very useful as a comment but it doesn't answer the question. There is a distinction. – djv Aug 06 '15 at 14:35