UnauthorizedAccessException cannot resolve Directo

2019-01-01 11:38发布

This question already has an answer here:

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.

7条回答
ら面具成の殇う
2楼-- · 2019-01-01 12:11

In .NET 4 this becomes a lot easier, see http://msdn.microsoft.com/en-us/library/dd997370.aspx

查看更多
登录 后发表回答