patterncsharpMinor
Find all file occurences of a search term on drive
Viewed 0 times
filesearchalltermoccurencesfinddrive
Problem
I have written a small application that lets the user search drives for their given input string. I am only returning the full name of files. What I have now is the following method to retrieve those full names:
Where I made the choice of using
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 was wondering if there is any other solution to increase performance even more, perhaps through a different API? Are there any other tweaks to my code you can suggest?
private IEnumerable FindOccurences(string searchQuery, DriveInfo drive)
{
return drive.RootDirectory.EnumerateFiles(searchQuery).Select(file => file.FullName);
}Where I made the choice of using
EnumerateFiles over GetFiles to get faster performance as shown here: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 was wondering if there is any other solution to increase performance even more, perhaps through a different API? Are there any other tweaks to my code you can suggest?
Solution
I really like this code. Let me explain why in a few short sentences:
Typechoice:
You return an
Naming:
Your names are short, comprehensive and follow the naming conventions. There's not much more to say.
Program flow:
It is instantly obvious to the reader, what your program does and how it does that, simply by speaking english. This is very hard to accomplish, and I want to compliment you on that.
Choice of methods:
You explained very nicely why you chose
Typechoice:
You return an
IEnumerable and not a List. This is awesome, because a List implies sorted entries and blocks some Types, that you can use with IEnumerableNaming:
Your names are short, comprehensive and follow the naming conventions. There's not much more to say.
Program flow:
It is instantly obvious to the reader, what your program does and how it does that, simply by speaking english. This is very hard to accomplish, and I want to compliment you on that.
Choice of methods:
You explained very nicely why you chose
EnumerateFiles() over GetFiles(). If you'd comment that, IMO your code would be perfect.Context
StackExchange Code Review Q#54792, answer score: 2
Revisions (0)
No revisions yet.