无法读取注册表:System.Security.SecurityException,不允许请求的注册表访问 [英] Unable to read registry: System.Security.SecurityException, Requested registry access is not allowed

查看:90
本文介绍了无法读取注册表:System.Security.SecurityException,不允许请求的注册表访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到收到错误System.Security.SecurityException,不允许请求的注册表访问"的用户报告的错误.尝试读取注册表时.我想不通为什么有人没有权限读取注册表,而且我无法在我的 Windows 7 PC 上重现该问题.受影响的用户正在运行 .NET 4.0

I'm getting reported errors from users who are receiving the error "System.Security.SecurityException, Requested registry access is not allowed." when trying to read the registry. I can't think why someone would not have permission to read the registry and I'm unable to reproduce the problem on my Windows 7 PC. Affected users are running .NET 4.0

这是我使用的 C# 代码:

Here's the C# code I'm using:

var baseReg = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
var key = baseReg.OpenSubKey(RegKey, RegistryKeyPermissionCheck.ReadSubTree);

if (key != null)
{
    var value = key.GetValue("DisableAutoUpdate", 0);
    if (value != null)
    {
        updatesDisabled = Convert.ToBoolean(value);
    }
}

我已经检查了受影响用户的相关注册表项的权限,并且标准用户对该项具有读取权限.

I've checked permissions on the registry key concerned for the affected user and standard Users have read permission for that key.

编辑 2 和解决方案:据受影响用户称,安装 .NET 4.5.2 可以解决问题!我不知道为什么.

EDIT 2 and SOLUTION: According to the affected user, installing .NET 4.5.2 resolves the problem! I'm not sure why.

感谢@nozzleman 在下面的回答,通过强制它以只读方式打开密钥来解决这个问题.然而,奇怪的是 .NET 4.0 作为 4.5.2 的行为似乎有所不同.

Thanks to @nozzleman's answer below this is fixed by forcing it to open the key as read-only. However it's odd that .NET 4.0 as 4.5.2 appear to behave differently.

推荐答案

OpenSubKey(..)-Method 的重载,允许添加第三个参数.您可以尝试将 RegistryRights.ReadKey 传递给那个,看看是否能解决问题.

There is an overload of the OpenSubKey(..)-Method that allows to add a third parameter. You could try passing RegistryRights.ReadKey with that one and see if that solves the issue.

baseReg.OpenSubKey(
    RegKey, 
    RegistryKeyPermissionCheck.ReadSubTree
    RegistryRights.ReadKey);

或者,尝试接受 2 个参数的其他重载像这样

Alternatively, try the other overload accepting 2 parameters like so

baseReg.OpenSubKey(RegKey, false); 

这会导致以只读方式打开子键,并且您不需要在给定的 szenario 中读取整个子树..

This leads to opening the subkey readonly, and you dont neet to read the whole sub tree in the given szenario..

这篇关于无法读取注册表:System.Security.SecurityException,不允许请求的注册表访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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