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.

clip_image002

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

clip_image004

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

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

2 thoughts on “New APIs in System.IO”

  1. 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.

Leave a comment