检查当前用户的共享文件夹写访问权限

时间:2011-08-12 06:20:14

标签: c# permissions c#-2.0

我有以下方法来检查当前用户是否具有对给定网络位置的写访问权

DirectorySecurity shareSecurity = new DirectoryInfo(this.GetFileServerRootPath).GetAccessControl();

foreach (FileSystemAccessRule fsRule in shareSecurity.GetAccessRules(true, true, typeof(NTAccount)))
{
    // check write permission for current user
    if (AccessControlType.Allow == fsRule.AccessControlType &&
            FileSystemRights.Write == (fsRule.FileSystemRights & FileSystemRights.Write))
    {
        if (null != fsRule.IdentityReference &&
            fsRule.IdentityReference.Value == WindowsIdentity.GetCurrent().Name)
        {
            return true;
        }
    }
}
return false;

但问题是当给予用户组的文件夹权限时,上面的方法失败了。

我不想通过编写文件来检查权限,并决定写访问权限。

有没有办法找到IdentityReference.Value中的当前用户?或建议克服这个问题?

2 个答案:

答案 0 :(得分:1)

这可能对您有用:

FileIOPermission writePermission = new FileIOPermission(FileIOPermissionAccess.Write, this.GetFileServerRootPath);

try
{
    writePermission.Demand();
    return true;
}
catch (SecurityException s)
{
    return false;
}

好奇 - 为什么不尝试/捕捉你的写操作?

答案 1 :(得分:0)

您可能应该使用该目录上的DirectoryInfo来获取其安全策略。