RegistryKeyPremissionsCheck和LocalMachine在当前上下文中不存在

本文关键字:上下文 不存在 LocalMachine RegistryKeyPremissionsCheck | 更新日期: 2023-09-27 18:25:14

我试着用UI编写一个简单的C#程序,在注册表中添加我自己的键和值。我把它添加到注册表中,这样我的程序可以在以后启动时读取它,而不会重新配置自己。

RegistryKey rk =  LocalMachine.OpenSubKey("HKEY_LOCAL_MACHINE''SOFTWARE''AMC", 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("HKEY_LOCAL_MACHINE''SOFTWARE''AMC", 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.

这是我从Stackoverflow的一些问题中获得的代码。我试图解决添加/删除/修改密钥时的权限问题。

我遗漏了什么吗?

RegistryKeyPremissionsCheck和LocalMachine在当前上下文中不存在

在第一行中,您使用的是RegistryKeyPremissionsCheck,这是一个尚未定义的变量。同样的情况进入CCD_ 2。写入线路

RegistryKey LocalMachine = Registry.LocalMachine;

在您的LocalMachine 代码之前

至于RegistryKeyPremissionsCheck.ReadWriteSubTree,如果你想编辑你打开的子密钥下的值,就用true代替它,否则你可以把false放在那里

如果你只是想添加注册表项,我会使用-

int MyNumber = 0; // Your value, doesnt have to be a number
Registry.SetValue("HKEY_LOCAL_MACHINE''SOFTWARE''AMC", "My User Name", MyNumber);

并获得值

object val = Registry.GetValue("HKEY_LOCAL_MACHINE''SOFTWARE''AMC", "My User Name", -1);

现在我放了这个代码,如果你有正确的权限(你是管理员),它就会工作

RegistryKey LocalMachine = Registry.LocalMachine;
         RegistryKey rk = LocalMachine.OpenSubKey("HKEY_LOCAL_MACHINE''SOFTWARE''AMC",
            RegistryKeyPermissionCheck.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("HKEY_LOCAL_MACHINE''SOFTWARE''AMC",
            RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl); // Opens the key again with full control.
         rs.SetOwner(new NTAccount("Administrator"));// Set the securities owner to be Administrator
         rk.SetAccessControl(rs);
         int MyNumber = 0; // Your value, doesn't have to be a number
         rk.SetValue("username", MyNumber);// The username should by the dynamic part