Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a reason why we would use Directory.GetFiles() over Directory.EnumerateFiles()?

I am not sure why we would use Directory.GetFiles for if Directory.EnumerateFiles would be able to do the same thing and you would be able to enumerate the list even before the whole list of directories found is returned.

What is the difference between Directory.EnumerateFiles vs Directory.GetFiles?

Why, now that EnumerateFiles is available would there be a need to use GetFiles?

like image 974
SamIAm Avatar asked Jul 04 '14 02:07

SamIAm


3 Answers

According to http://msdn.microsoft.com/en-us/library/07wt70x2%28v=vs.110%29.aspx:

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.

I guess GetFiles could be considered as a convenience function.

like image 87
AlexD Avatar answered Oct 09 '22 15:10

AlexD


You use the right tool for the right job. If you have a small number of files, then you probably want to just use Directory.GetFiles() and load them into memory.

Directory.EnumerateFiles() on the other hand is when you need to conserve memory by enumerating the files one at a time.

This is similar to the argument of whether to make everything lazy, or just use it when appropriate. I think this helps put things into perspective because making everything lazy for no good reason is overkill.

You have the trade off of simplicity vs efficiency. Conceptually, an enumerator which doesn't actually contain the files, but yields them one at a time is more complex than a string array. Being able to store that enumerator in an IEnumerable<string> is a wonderful abstraction, but the point remains.

It may also be more difficult to predict performance. With GetFiles() you know the performance cost is incurred when the method is called. EnumerateFiles() does not do anything until it is enumerated.

Intention is important. If you are using EnumerateFiles() on a directory that you know won't have that many files, then you're just using it blindly without understanding. The computer the code runs on matters, too. If I am running something on a server with 24 GB of RAM then I may be more likely to use memory-hungry methods like GetFiles() than if I was on a server with 1 GB of RAM.

If you have no idea how many files there will be or unsure what kind of environment the code will run on, then it would be wise to err on the side of caution and use the memory-efficient version.

There are many variables to consider and a good programmer takes these things into consideration and does things purposefully.

like image 32
Despertar Avatar answered Oct 09 '22 15:10

Despertar


Both of these 2 methods ultimately make a call to System.IO.FileSystemEnumerableFactory.CreateFileNameIterator(). The only difference seems to be that the GetFiles() result gets wrapped in a List and then turned into an array. So as mentioned in some other answers, the difference is that the enumerator has been traversed already via GetFiles() versus EnumerateFiles which gives you the enumerator before traversing it.

For reference I was able to find this using ILSpy (http://ilspy.net/)

like image 41
bingles Avatar answered Oct 09 '22 17:10

bingles