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