Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

When searching the C Drive for MP3's I get an access denied error message when it tries to access the Recycle Bin...

$
0
0

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)
                    {
                        
                    }
                }
            }
        }



Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>