gpt4 book ai didi

c# - RegistrySecurity 访问被拒绝。 C#

转载 作者:太空狗 更新时间:2023-10-29 21:46:56 27 4
gpt4 key购买 nike

我目前在编写应用程序以设置某些旧 key 的权限时遇到问题。遗留 key 非常锁定,要在 regedit 中实际修改它们,您必须取得所有权,然后添加自己的完全控制权。当试图在代码中复制它时,我无法获得写入 key ,并出现错误“访问被拒绝”。示例代码:

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);

任何想法将不胜感激。我也尝试过请求写入权限的 OpenSubKey,但我就是无法获得 key 。

谢谢你们。

最佳答案

我终于找到了解决办法。您必须使用“ChangePermissions”打开 key ,然后为自己更改权限...然后重新打开具有完全控制权的 key 以更改所有者。方法如下。

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.

这对我有用。让我知道它是否适合你:)

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

关于c# - RegistrySecurity 访问被拒绝。 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6455691/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com