6
votes

I have Application settings stored under HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany branch. Settings must be same for different users and that is the reason why settings are not under HKEY_CURRENT_USER. Registry values are only read during use of application.

Now, in Windows Vista and due to UAC you can't anymore use following code to read registry values:

RegistryKey myKey = Registry.LocalMachine.CreateSubKey
        ("SOFTWARE\\MyCompany\\MyAppName");

How can I read the values from LocalMachine branch in my code (C#)?

1

1 Answers

7
votes

The problem is that you are trying to create a key not read it. You should be able to read values from HKLM just fine on Vista if you use the appropriate API.

RegistryKey myKey = Registry.LocalMachine.OpenSubKey(
  @"Software\MyCompany\MyAppName", 
  false);

Notice the false parameter in the above. This has the effect of opening the key in a read only mode. This is the default setting for OpenSubKey but I prefer to be explicit (mainly because I can't ever remember the default).