检查目录是否可以在 C# 中访问?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11709862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if directory is accessible in C#?
提问by user962284
Possible Duplicate:
.NET - Check if directory is accessible without exception handling
可能的重复:
.NET - 检查目录是否可访问而无需异常处理
Im making a small file explorer in Visual Studio 2010 with NET 3.5 and C#, and I have this function to check if a directory is accessible:
我使用 NET 3.5 和 C# 在 Visual Studio 2010 中制作了一个小文件浏览器,我有这个功能来检查目录是否可访问:
RealPath=@"c:\System Volume Information";
public bool IsAccessible()
{
//get directory info
DirectoryInfo realpath = new DirectoryInfo(RealPath);
try
{
//if GetDirectories works then is accessible
realpath.GetDirectories();
return true;
}
catch (Exception)
{
//if exception is not accesible
return false;
}
}
But I think with big directories it could be slow trying to get all sub directories to check if directory is accesible. Im using this function to prevent errors when trying to explore protected folders or cd/dvd drives without disc ("Device Not Ready" error).
但我认为对于大目录,尝试让所有子目录检查目录是否可访问可能会很慢。我使用此功能来防止在尝试探索受保护的文件夹或没有光盘的 cd/dvd 驱动器时出错(“设备未就绪”错误)。
Is there a better way (faster) to check if directory is accessible by the application (preferably in NET 3.5)?
有没有更好的方法(更快)来检查应用程序是否可以访问目录(最好在 NET 3.5 中)?
采纳答案by Chibueze Opata
According to MSDN, Directory.Existsshould return false if you don't have read access to the directory. However, you can use Directory.GetAccessControlfor this. Example:
根据MSDN,Directory.Exists如果您没有目录的读取权限,则应返回 false 。但是,您可以Directory.GetAccessControl为此使用。例子:
public static bool CanRead(string path)
{
try
{
var readAllow = false;
var readDeny = false;
var accessControlList = Directory.GetAccessControl(path);
if(accessControlList == null)
return false;
//get the access rules that pertain to a valid SID/NTAccount.
var accessRules = accessControlList.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
if(accessRules ==null)
return false;
//we want to go over these rules to ensure a valid SID has access
foreach (FileSystemAccessRule rule in accessRules)
{
if ((FileSystemRights.Read & rule.FileSystemRights) != FileSystemRights.Read) continue;
if (rule.AccessControlType == AccessControlType.Allow)
readAllow = true;
else if (rule.AccessControlType == AccessControlType.Deny)
readDeny = true;
}
return readAllow && !readDeny;
}
catch(UnauthorizedAccessException ex)
{
return false;
}
}
Update
更新
As mentioned in some comments, this may return an incorrect value in a case where a valid SID in an external DOMAIN has access. In order to check if the current user has access, you need something like:
正如一些评论中提到的,在外部域中的有效 SID 具有访问权限的情况下,这可能会返回不正确的值。为了检查当前用户是否具有访问权限,您需要以下内容:
foreach...
if (WindowsIdentity.GetCurrent().User.Value.equals(rule.IdentityReference.Value))
This will confirm if the SID of the current user matches the access rule identity reference but may throw a SecurityException as well.
这将确认当前用户的 SID 是否与访问规则身份引用匹配,但也可能抛出 SecurityException。
回答by Rakesh
I think you are looking for the GetAccessControlmethod, the System.IO.File.GetAccessControlmethod returns a FileSecurity object that encapsulates the access control for a file.
我认为您正在寻找该GetAccessControl方法,该System.IO.File.GetAccessControl方法返回一个 FileSecurity 对象,该对象封装了文件的访问控制。

