Took from here
private List<string> GetFiles(string path, string pattern) { var files = new List<string>(); try { files.AddRange(Directory.GetFiles(path, pattern, SearchOption.TopDirectoryOnly)); foreach (var directory in Directory.GetDirectories(path)) files.AddRange(GetFiles(directory, pattern)); } catch (UnauthorizedAccessException) { } return files; }
Recursive function When compiling the list of files, ordinary folders will be taken into account, and the system folders will not be taken into account. There should not be such that, due to the Exception, ordinary files will not be included in the final result.
Console.WriteLine("Started: " + DateTime.Now.ToLongTimeString()); var fileList = GetFiles("c:\\", "*.*"); Console.WriteLine("Finished: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("Files count: " + fileList.Count); // У меня так: // Started: 12:43:05 // Finished: 12:44:33 // Files count: 427083