New APIs in System.IO

Have a look at some of the new APIs in introduced in System.IO which makes some of the tasks efficient – both performance as well as memory wise.

For example, there are instances when we need the total number of files in a given directory (recursively). In those instances, we think of following two methods :-

System.IO.DirectoryInfo.GetFiles() which returns FileInfo[]

System.IO.Directory.GetFiles() which returns string[] (which contains names)

.NET 4.0 introduced some new methods for IO and two of which are :-

System.IO.DirectoryInfo.EnumerateFiles() which returns IEnumerable<FileInfo>

System.IO.Directory. EnumerateFiles() which returns IEnumerable<string> (which contains names)

Why these two new methods? Because they are efficient because when we use them we don’t have to wait for thw whole FileInfo[] or String[] to return before we could access the collection. From MSDN:-

TheEnumerateFilesandGetFilesmethods differ as follows: When you useEnumerateFiles, you can start enumerating the collection ofFileInfoobjects before the whole collection is returned; when you useGetFiles, you must wait for the whole array ofFileInfoobjects to be returned before you can access the array. Therefore, when you are working with many files and directories,EnumerateFilescan be more efficient.”

I did some performance comparison ( a small project attached ) and following are the results :-

For 56505 number of files :-

Methods Time taken (in milliseconds) Memory used (in Kilobytes)
DirectoryInfo.GetFiles() 3393 31004
DirectoryInfo.EnumerateFiles() 3365 6223
Directory.GetFiles() 3001 24888
Directory.EnumerateFiles() 2961 6992

As you can see DirectoryInfo.EnumerateFiles() is faster than DirectoryInfo.GetFiles() and more importantly occupies almost 1/5 of memory in comparison.

Also, regarding Directory.GetFiles(), It is used to fetch the names of the files. But as we can see one can use Directory.EnumerateFiles() which is faster and occupies almost 1/4th of the memory in comparison.


[Task manager showing memory occupied by DirectoryInfo.GetFiles()]


[Task manager showing memory occupied by DirectoryInfo.EnumerateFiles()]

Therefore, when we need to enumerate large number of files, we can look at EnumerateFiles() method.

  1. #1 by jay on May 13, 2011 - 2:06 am

    Hi, i m trying to create a windows form in VS2010 and i m trying to use System.IO.DirectoryInfo.EnumerateFiles() in my code but i m getting error saying:” System.IO.DirectoryInfo’ does not contain a definition for ‘EnumerateFiles’ and no extension method ‘EnumerateFiles’ accepting a first argument of type ‘System.IO.DirectoryInfo’ could be found (are you missing a using directive or an assembly reference?). i m struggleing to slove this error. is VS2010 is not .Net Framework 4?. please help how can reslove this and use that method.

  2. #2 by ashishmgupta on May 13, 2011 - 3:06 am


    Make sure under the project properties, you have “.NET Framework 4” selected under the “Target Framework”.

    Hope this helps,

Leave a Reply to ashishmgupta Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Random Thoughts

The World as I see it

Simple Programmer

Making The Complex Simple

Ionic Solutions

Random thoughts on software construction, design patterns and optimization.

Long (Way) Off

A tragic's view from the cricket hinterlands

%d bloggers like this: