修改c#中的现有注册表项值
我想使用Windows 7中的C#代码修改注册表路径SOFTWARE\Wow6432Node\Program\SubProgram
中的数据。我能够读取值,但我无法写入Registry 。 这是代码:
RegistryKey SUBKEY; RegistryKey TAWKAY = RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, ""); string subkey = "SOFTWARE\\Wow6432Node\\Program\\SubProgram "; if (TAWKAY.OpenSubKey(subkey) != null) // Get values from Registry { TAWKAY = RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, ""); SUBKEY = TAWKAY.OpenSubKey(subkey); // subkey opens SUBKEY = TAWKAY.OpenSubKey(subkey,true); // subkey not open shows error Requested registry access is not allowed SUBKEY.SetValue("Some name", "1234567890"); Console.WriteLine(SUBKEY.GetValue("Some name").ToString()); } else { Console.WriteLine("Cannot open registry"); } Console.Read();
如果我设置OpenSubKey(subkey, true)
,它会显示错误消息Requested registry access is not allowed
是否需要写入注册表的权限 ? 请帮我解决这个问题
Wow6432Node不是注册表中的真实路径。 它是64位OS中32位密钥的别名。
您必须使用RegistryView.Registry32才能指定您希望使用32位。
RegistryKey reg32key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32); RegistryKey reg_32bit_AppKey = reg32key.OpenSubKey(@"SOFTWARE\Program\SubProgram"); if (reg_32bit_AppKey != null) { // Here you can work with "SOFTWARE\\Wow6432Node\\Program\\SubProgram " }
在HKLM中修改/删除/添加密钥需要管理员权限。
在这种情况下,您需要将应用程序清单的requestedExecutionLevel
值更改为requireAdministrator
最好使用“Reg”命令以便在注册表上执行任何操作。
即使您想要访问远程计算机的注册表,您也不需要该计算机的凭据,因此拥有该计算机名称就足够了。
有关“REG”命令的更多信息,请参阅以下链接
http://technet.microsoft.com/en-us/library/cc732643(v=ws.10).aspx