简体   繁体   English

将注册表访问规则添加到我无权访问但我是管理员的密钥

[英]Add registry access rule to key to which I've no access but I'm an administrator

I'm trying to add some access rules to registry key to which I've no rights, but I'm an administrator and using "Run as administrator" command at right mouse button menu. 我想添加一些访问规则注册表来我已经没有权利的关键,但我是管理员,并使用"Run as administrator" ,在鼠标右键菜单命令。
Unfortunately exception is being thrown ( System.UnauthorizedAccessException ). 不幸的是,抛出了异常( System.UnauthorizedAccessException )。
When running regedit.exe as administrator I can change rights to this key w/o any problems. 当以管理员身份运行regedit.exe ,我可以更改此注册表项的权限,而不会出现任何问题。
How to add any access rule to this key in my application? 如何任何访问规则添加到我的应用程序如此重要呢?

RegistryKey root = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\some_key", RegistryKeyPermissionCheck.ReadWriteSubTree);
RegistrySecurity security = new RegistrySecurity();
SecurityIdentifier sec = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
RegistryAccessRule rule = new RegistryAccessRule(sec, RegistryRights.ReadKey | RegistryRights.QueryValues, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);
security.AddAccessRule(rule);
root.SetAccessControl(security);
root.Close();

I know this is an old thread, but I've encountered this problem before. 我知道这是一个旧线程,但是我之前遇到过此问题。 I found that you need to first open the key with RegistryRights.ChangePermissions , then you can modify the access control. 我发现您首先需要使用RegistryRights.ChangePermissions打开密钥,然后才能修改访问控制。

Try opening the key like this: 尝试像这样打开密钥:

    RegistryKey root = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\some_key",
RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.ChangePermissions);

And then modifying the AccessControl rules. 然后修改AccessControl规则。

I think you should use the GetAccessControl () method to get a RegistrySecurity object: 我认为您应该使用GetAccessControl ()方法来获取RegistrySecurity对象:

RegistryKey root = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\some_key", RegistryKeyPermissionCheck.ReadWriteSubTree);
RegistrySecurity security = root.GetAccessControl();

You probably get the exception because you are trying to overwrite the security rules, instead of modifying them. 您可能会得到例外,因为您试图覆盖安全规则,而不是对其进行修改。

The documentation is pretty clear: 文档非常清楚:

UnauthorizedAccessException : The current RegistryKey object represents a key with access control security, and the caller does not have RegistryRights.ChangePermissions rights. UnauthorizedAccessException :当前的RegistryKey对象表示具有访问控制安全性的密钥,并且调用方没有RegistryRights.ChangePermissions权限。

See also the documentation for RegistryRights . 另请参见RegistryRights文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM