RegistrySecurity访问被拒绝.C#

Ski*_*gle 6 c# security registry

我在编写应用程序以设置某些旧版密钥的权限时遇到问题.旧版密钥已完全锁定,并且在regedit中实际修改它们,您必须拥有所有权,然后添加完全控制权.当试图在代码中复制它时,我无法获得写入错误"访问被拒绝"的密钥.示例代码:

RegistrySecurity rs = new RegistrySecurity();
rs.AddAccessRule(new RegistryAccessRule("Administrators", RegistryRights.FullControl, AccessControlType.Allow));
rs.SetOwner(new NTAccount("Administrators"));
return LocalMachine.CreateSubKey(post, RegistryKeyPermissionCheck.ReadWriteSubTree, rs);
Run Code Online (Sandbox Code Playgroud)

任何想法将不胜感激.我也尝试过OpenSubKey请求写访问权限,但我无法获得密钥.

多谢你们.

Ski*_*gle 7

我终于找到了解决方案.您必须使用"ChangePermissions"打开密钥,然后更改自己的权限...然后重新打开密钥并完全控制以更改所有者.这是如何做.

RegistryKey rk = LocalMachine.OpenSubKey(subkey, RegistryKeyPremissionsCheck.ReadWriteSubTree, RegistryRights.ChangePermissions | RegistryRights.ReadKey);//Get the registry key desired with ChangePermissions Rights.
RegistrySecurity rs = new RegistrySecurity();
rs.AddAccessRule(new RegistryAccessRule("Administrator", RegistryRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));//Create access rule giving full control to the Administrator user.
rk.SetAccessControl(rs); //Apply the new access rule to this Registry Key.
rk = LocalMachine.OpenSubKey(subkey, RegistryKeyPremissionsCheck.ReadWriteSubTree, RegistryRights.FullControl); // Opens the key again with full control.
rs.SetOwner(new NTAccount("Administrator"));// Set the securitys owner to be Administrator
rk.SetAccessControl(rs);// Set the key with the changed permission so Administrator is now owner.
Run Code Online (Sandbox Code Playgroud)

这适合我.请让我知道这对你有没有用 :)

如果您没有以管理员身份登录或者您需要其他用户的权限,显然会将管理员更改为其他用户.