.NET - 检查目录是没有异常处理访问 [英] .NET - Check if directory is accessible without exception handling

查看:145
本文介绍了.NET - 检查目录是没有异常处理访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要去通过各种目录的计算机(通过DirectoryInfo的)上。其中有些是无法访问的,并UnauthorizedAccessException发生。如何检查目录的访问没有捕捉异常?

I need to go through various directories on the computer (via DirectoryInfo). Some of them aren't accessible, and UnauthorizedAccessException occurs. How can I check directory access without catching the exception?

推荐答案

您需要使用安全命名空间。

请参阅<一href="http://stackoverflow.com/questions/130617/how-do-you-check-for-permissions-to-write-to-a-directory-or-file">this这样,请回答。

这是问题的答案:

FileIOPermission writePermission = new FileIOPermission(FileIOPermissionAccess.Write, filename);
if(!SecurityManager.IsGranted(writePermission))
{
  //No permission. 
  //Either throw an exception so this can be handled by a calling function
  //or inform the user that they do not have permission to write to the folder and return.
}

更新:(以下评论)

的FileIOPermission 安全策略的交易不是文件系统的权限,所以你需要使用的 DirectoryInfo.GetAccessControl

FileIOPermission deals with security policies not filesystem permissions, so you need to use DirectoryInfo.GetAccessControl.

这篇关于.NET - 检查目录是没有异常处理访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆