This is just a permissions issue. Use a try / catch block. Some of the folders on your drive, including the RecycleBin folders, are not available for unprivileged code.
public List<string> Search() { var files = new List<string>(); foreach (DriveInfo d in DriveInfo.GetDrives().Where(x => x.IsReady)) { try { files.AddRange(Directory.GetFiles(d.RootDirectory.FullName, "*.txt", SearchOption.AllDirectories)); } catch(Exception e) { Logger.Log(e.Message);
Also note that using the Directory.GetFiles option with the AllDirectories parameter has an inherent problem, which will fail if ANY of the folders on the entire drive is inaccessible, and therefore you will not receive any files for this drive in your Results. The solution is to do manual recursion. A great example of this approach is this SO question .
source share