UnauthorizedAccessException cannot resolve Directory.GetFiles failure

Posted by Ric Coles on Stack Overflow See other posts from Stack Overflow or by Ric Coles
Published on 2009-09-08T10:23:45Z Indexed on 2010/04/18 20:13 UTC
Read the original article Hit count: 391

Hi all,

Directory.GetFiles method fails on the first encounter with a folder it has no access rights to.

The method throws an UnauthorizedAccessException (which can be caught) but by the time this is done, the method has already failed/terminated.

The code I am using is listed below:

        try
        {
            // looks in stated directory and returns the path of all files found                
            getFiles = Directory.GetFiles(@directoryToSearch, filetype, SearchOption.AllDirectories);             
        }
        catch (UnauthorizedAccessException) { }

As far as I am aware, there is no way to check beforehand whether a certain folder has access rights defined.

In my example, I'm searching on a disk across a network and when I come across a root access only folder, my program fails.

I have searched for a few days now for any sort of resolve, but this problem doesn't seem to be very prevalent on here or Google.

I look forward to any suggestions you may have, Regards

© Stack Overflow or respective owner

Related posts about c#

Related posts about exception-handling