在 C# 中修改现有的注册表项值 [英] Modify an existing registry key value in c#

查看:106
本文介绍了在 C# 中修改现有的注册表项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Windows 7 中使用 C# 代码修改注册表路径 SOFTWARE\Wow6432Node\Program\SubProgram 中的数据.我能够读取该值,但我无法写入注册表.代码如下:

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

如果我设置了OpenSubKey(subkey, true),它会显示一个错误信息Requested registry access is not allowed

If I set OpenSubKey(subkey, true), it shows an error message Requested registry access is not allowed

是否需要任何权限才能写入注册表?请帮我解决问题

推荐答案

Wow6432Node 不是注册表中的真实路径.它是 64 位操作系统中 32 位密钥的别名.

Wow6432Node is not a real path in the registry. It is an alias for 32 bit keys in 64 bit OS.

您必须使用 RegistryView.Registry32 才能指定要使用 32 位.

You must use RegistryView.Registry32 in order to specify you want to work with 32 bits.

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 "
}

这篇关于在 C# 中修改现有的注册表项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆