Optimize search through all drives
123244
60
Reputation points
Hi, I need to implement an efficient search through user's disks. I would really like to search multiple disks in parallel to speed up the process, but at the same time, avoid using too much CPU resources.
I've already implemented a directory search that will scan a directory first superficially (only the current directory), and then, if necessary, will also scan subdirectories. Here is an example:
public async Task SearchAsync(SearchOptions searchOptions)
{
// Look in searched directory
await ShallowSearchAsync(searchOptions);
// If nested search needs to be performed, look also in subdirectories
if (searchOptions.Filter.IsNestedSearch)
{
await Task.Run(async () =>
{
foreach(subdirectory in EnumerateSubDirectories())
{
await subdirectory.SearchAsync(searchOptions);
}
}, searchOptions.Token);
}
}
public async Task ShallowSearchAsync(SearchOptions searchOptions)
{
// Here we look through directorie's files
// and filter out those that do not meet the search conditions
}
If you have any ideas on how to optimize my search method, I'd be glad to hear them.
Sign in to answer