Make Directory.GetFiles() ignore protected folders

Posted by Kryptic on Stack Overflow See other posts from Stack Overflow or by Kryptic
Published on 2011-02-14T22:58:16Z Indexed on 2011/02/14 23:25 UTC
Read the original article Hit count: 236

Filed under:
|
|
|

Hello Everyone,

I'm using the Directory.GetFiles() method to get a list of files to operate on. This method throws an UnauthorizedAccessException for example when trying to access a protected folder. I would like it to simply skip over such folders and continue. How can I accomplish this with either Directory.GetFiles (preferably) or another method?

Update:

Here is the code that throws the exception. I am asking the user to select a directory and then retrieving the list of files. I commented out the code (so this is now whole method) that iterates through the files and the problem still occurs. The exception is thrown on the Directory.GetFiles() line.

FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult dr = fbd.ShowDialog();

if (dr == System.Windows.Forms.DialogResult.Cancel) return; 

string directory = fbd.SelectedPath;
string[] files = Directory.GetFiles(directory, "*.html", SearchOption.AllDirectories);

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET