<分区>
Directory.GetFiles method第一次遇到它没有访问权限的文件夹时失败。
该方法抛出一个 UnauthorizedAccessException(可以被捕获),但在完成时,该方法已经失败/终止。
我使用的代码如下:
try
{
// looks in stated directory and returns the path of all files found
getFiles = Directory.GetFiles(
@directoryToSearch,
filetype,
SearchOption.AllDirectories);
}
catch (UnauthorizedAccessException)
{
}
据我所知,没有办法事先检查某个文件夹是否定义了访问权限。
在我的示例中,我正在通过网络搜索磁盘,当我遇到一个仅限根访问的文件夹时,我的程序失败了。