Add a key to the registry if it does not exist

I am trying to add a key to the registry if it does not exist. While I'm debugging everything is fine. The code should work. But I can not find the key in the registry editor. Do you have any ideas?

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"); } } 
+5
source share
1 answer

If you use a 64-bit OS, some registry keys are redirected to WOW64. Additional information on this topic is available on MSDN , you should look under Wow6432Node, and you will find your entry. If you execute this code the first time it is created on a 64-bit machine (I tried it locally), this entry:

HKEY_LOCAL_MACHINE \ Software \ Wow6432Node \ Microsoft \ Office \ Outlook \ FormRegions \ tesssst

if you want to access your 64-bit registry section, which you should do:

 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 above code will put the registry key in the correct section that you are targeting.

hope this helps.

+7
source

Source: https://habr.com/ru/post/1215742/


All Articles