I am in the process of writing a small method which retrieves all full filenames of a given input string and a given drive. My method as follows:
private IEnumerable<string> FindOccurences(string searchQuery, DriveInfo drive)
{
return drive.RootDirectory
.EnumerateFiles("*" + searchQuery + "*", SearchOption.AllDirectories)
.Where(file => !file.FullName.Contains("RECYCLE"))
.Select(file => file.FullName);
}
While everything works fine on drives that do not have a Recycle Bin, my C:\
and D:\
Directoryraise UnauthorisedAccessException
when enumerating through the files and into the Recycle Bin. This same problem is explained and used in this topic, but does not use LINQ to solve it.
I was hoping there would be a solution using LINQ to skip over the Recycle Bin and move on to the other directories. Is this possible? As you can see I already attempted something in the form of checking for name, but this did not solve my problem.
(How) can I skip over the Recycle Bin using LINQ?
Edit: I actually found an occurance in my C:\ Directory (OS Directory) as well: C:\$Recycle.Bin