For file and directory search purpose I would want to offer use multithreading .NET library that possess a wide search opportunities. All information about library you can find on GitHub: https://github.com/VladPVS/FastSearchLibrary If you want to download it you can do it here: https://github.com/VladPVS/FastSearchLibrary/releases If you have any questions please ask them.
Works really fast. Check it yourself!
It is one demonstrative example how you can use it:
class Searcher { private static object locker = new object(); private FileSearcher searcher; List<FileInfo> files; public Searcher() { files = new List<FileInfo>(); } public void Startsearch() { CancellationTokenSource tokenSource = new CancellationTokenSource(); searcher = new FileSearcher(@"C:\", (f) => { return Regex.IsMatch(f.Name, @".*[Dd]ragon.*.jpg$"); }, tokenSource); searcher.FilesFound += (sender, arg) => { lock (locker) // using a lock is obligatorily { arg.Files.ForEach((f) => { files.Add(f); Console.WriteLine($"File location: {f.FullName}, \nCreation.Time: {f.CreationTime}"); }); if (files.Count >= 10) searcher.StopSearch(); } }; searcher.SearchCompleted += (sender, arg) => { if (arg.IsCanceled) Console.WriteLine("Search stopped."); else Console.WriteLine("Search completed."); Console.WriteLine($"Quantity of files: {files.Count}"); }; searcher.StartSearchAsync(); } }
It's part of other example:
*** List<string> folders = new List<string> { @"C:\Users\Public", @"C:\Windows\System32", @"D:\Program Files", @"D:\Program Files (x86)" }; // list of search directories List<string> keywords = new List<string> { "word1", "word2", "word3" }; // list of search keywords FileSearcherMultiple multipleSearcher = new FileSearcherMultiple(folders, (f) => { if (f.CreationTime >= new DateTime(2015, 3, 15) && (f.Extension == ".cs" || f.Extension == ".sln")) foreach (var keyword in keywords) if (f.Name.Contains(keyword)) return true; return false; }, tokenSource, ExecuteHandlers.InCurrentTask, true); ***
Moreover one can use simple static method:
List<FileInfo> files = FileSearcher.GetFilesFast(@"C:\Users", "*.xml");
Note that all methods of this library DO NOT throw UnauthorizedAccessException instead standard .NET search methods.
Furthermore fast methods of this library are performed at least in 2 times faster than simple one-thread recursive algorithm if you use multicore processor.