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