[英]Add key to registry if not exist
I try to add a key to the registry if not exist. 如果不存在,我尝试向注册表添加密钥。 While I debug everything is fine.
虽然我调试一切都很好。 Code should work.
代码应该工作。 But I can't find key in registry editor.
但我在注册表编辑器中找不到密钥。 Do you have any idea?
你有什么主意吗?
public void ConfigureWindowsRegistry()
{
var reg = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Office\\Outlook\\FormRegions\\tesssst", true);
if (reg == null)
{
reg = Registry.LocalMachine.CreateSubKey("Software\\Microsoft\\Office\\Outlook\\FormRegions\\tesssst");
}
if (reg.GetValue("someKey") == null)
{
reg.SetValue("someKey", "someValue");
}
}
If you are using a 64bit OS some registry keys are redirected by WOW64. 如果您使用的是64位操作系统,则WOW64会重定向某些注册表项。 More information on this topic is available on MSDN , you should look under Wow6432Node and you will find your entry.
有关此主题的更多信息可在MSDN上获得 ,您应该在Wow6432Node下查找,您将找到您的条目。 If you execute that code the first time it will create, on a 64 bit machine (I tried it locally), this entry:
如果你第一次在64位机器上创建该代码(我在本地试过),这个条目:
HKEY_LOCAL_MACHINE\\Software\\Wow6432Node \\Microsoft\\Office\\Outlook\\FormRegions\\tesssst
HKEY_LOCAL_MACHINE \\ Software \\ Wow6432Node \\ Microsoft \\ Office \\ Outlook \\ FormRegions \\ tesssst
if you want to access your 64 bit section of the registry you should do: 如果你想访问注册表的64位部分,你应该这样做:
public void ConfigureWindowsRegistry()
{
RegistryKey localMachine = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64); //here you specify where exactly you want your entry
var reg = localMachine.OpenSubKey("Software\\Microsoft\\Office\\Outlook\\FormRegions\\tesssst",true);
if (reg == null)
{
reg = localMachine.CreateSubKey("Software\\Microsoft\\Office\\Outlook\\FormRegions\\tesssst");
}
if (reg.GetValue("someKey") == null)
{
reg.SetValue("someKey", "someValue");
}
}
Executing the code above will put the registry key in the correct section you are targeting. 执行上面的代码会将注册表项放在您要定位的正确部分中。
hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.