C# 修改注册表键值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8816178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:35:59 来源:igfitidea点击:
modifying the registry key value
提问by Harish Kumar
I have a registry path of the following
我有以下注册表路径
HKEY_LOCAL_MACHINE\SOFTWARE\COMPANY\COMPFOLDER
inside COMPFOLDER, I have a string value called "Deno" whose value is 0. I wish to change its value to 1 by code whenever I execute the code. Can anyone help me?
在里面COMPFOLDER,我有一个名为“Deno”的字符串值,其值为 0。我希望在执行代码时通过代码将其值更改为 1。谁能帮我?
回答by Jontatas
It's been a while I did reg hacks, but something like this could work:
我已经有一段时间做了 reg hacks,但这样的事情可以工作:
RegistryKey myKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Company\Compfolder", true);
if(myKey != null) {
myKey.SetValue("Deno", "1", RegistryValueKind.String);
myKey.Close();
}
回答by electricalbah
using (RegistryKey key = regKeyRoot.OpenSubKey(KeyName, true)) //must dispose key or use "using" keyword
{
if (key != null) //must check for null key
{
key.SetValue(attribute, value);
}
}

