I'm using C# and Windows 8.1 along with Directory.GetFiles(folderBrowserDialog.SelectedPath, "*.mp3", SearchOption.AllDirectories). When I search the C:\ Drive I get an 'Access to the path 'C:\$Recycle.Bin\S-1-5-18' is denied.' error. I'm not worried about being able to access the Recycle Bin or other denied paths as I am skipping over the errors and continuing searching through the C:\ Drive. After any access error the foreach loop stops searching. How can I avoid the errors and continue seaching. Here's my code:
private void toolStripButtonSearch_Click(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialogMain.ShowDialog();
if(result == DialogResult.OK)
{
foreach (string file in Directory.GetFiles(folderBrowserDialogMain.SelectedPath, "*.mp3", SearchOption.AllDirectories))
{
try
{
FileInfo fi = new FileInfo(file);
ListViewItem lvi = new ListViewItem(fi.Name);
listViewMain.Items.Add(lvi);
}
catch (UnauthorizedAccessException)
{
}
}
}
}