I want to get a list of files that are in nested directories and use:

Directory.GetFiles(string,string,SearchOption) 

But after a while, I get the error: "Access is denied."

I looked in the debugger and found that Directory.GetFiles stumbles upon some hidden system folders that I basically don’t need.

Can I somehow ask to ignore these folders?

If I wrap everything in try/catch , then I'm afraid that I won't get the list I need.

    1 answer 1

    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