我得到一个UnautorizedAccessException
运行这段代码:
string[] fileList = Directory.GetFiles(strDir, strExt);
异常发生在c:\users\username\appdata
如何检查我是否具有访问权限(列出和读取文件)?
首先,我会手动检查权限,看看什么会阻止你,什么不会。我正在使用类似的方法来检查权限(对于复制文件):
AuthorizationRuleCollection acl = fileSecurity.GetAccessRules(true, true,typeof(System.Security.Principal.SecurityIdentifier));
bool denyEdit = false;
for (int x = 0; x < acl.Count; x++)
{
FileSystemAccessRule currentRule = (FileSystemAccessRule)acl[x];
AccessControlType accessType = currentRule.AccessControlType;
//Copy file cannot be executed for "List Folder/Read Data" and "Read extended attributes" denied permission
if (accessType == AccessControlType.Deny && (currentRule.FileSystemRights & FileSystemRights.ListDirectory) == FileSystemRights.ListDirectory)
{
//we have deny copy - we can't copy the file
denyEdit = true;
break;
}
... more checks
}
此外,还有一些奇怪的情况,文件夹上的某个权限会更改文件的权限,无论其各自的权限如何(将看看我是否能找到它是什么)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)