Optimize search through all drives

123244 60 Reputation points
2024-08-26T08:26:39.12+00:00

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.

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,498 questions
Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
794 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,012 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.