简体   繁体   English

修改c#中的现有注册表项值

[英]Modify an existing registry key value in c#

I want to modify a data in registry path SOFTWARE\\Wow6432Node\\Program\\SubProgram using C# code in windows 7. I am able to read the value but I can't write into Registry . 我想使用Windows 7中的C#代码修改注册表路径SOFTWARE\\Wow6432Node\\Program\\SubProgram中的数据。我能够读取值,但我无法写入Registry Here is the code: 这是代码:

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

If I set OpenSubKey(subkey, true) , it shows an error message Requested registry access is not allowed 如果我设置OpenSubKey(subkey, true) ,它会显示一条错误消息, Requested registry access is not allowed

Is there any permission needed to write into registry ? 是否需要写入注册表的权限 Please help me to solve the issue 请帮我解决这个问题

Wow6432Node is not a real path in the registry. Wow6432Node不是注册表中的真实路径。 It is an alias for 32 bit keys in 64 bit OS. 它是64位OS中32位密钥的别名。

You must use RegistryView.Registry32 in order to specify you want to work with 32 bits. 您必须使用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 "
}

Modifying/deleting/adding keys in HKLM requires administrator rights. 在HKLM中修改/删除/添加密钥需要管理员权限。

In that case you want to do that you will need to change your applications manifest requestedExecutionLevel value to requireAdministrator 在这种情况下,您需要将应用程序清单的requestedExecutionLevel值更改为requireAdministrator

It is better to use "Reg" command inorder to perform any operation on registry. 最好使用“Reg”命令以便在注册表上执行任何操作。

Even though if you want to access the registry of remote machine you don't nedd credentials of that machine, having the machine name is sufficient. 即使您想要访问远程计算机的注册表,您也不需要该计算机的凭据,因此拥有该计算机名称就足够了。

For more information about "REG" command refer to the following link 有关“REG”命令的更多信息,请参阅以下链接

http://technet.microsoft.com/en-us/library/cc732643(v=ws.10).aspx http://technet.microsoft.com/en-us/library/cc732643(v=ws.10).aspx

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

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