36

I have a windows service that attempt to write to a registry key in LOCAL_MACHINE

The key is created as part of a windows installer package the controls the windows service and a stand alone control window.

The control window can read and write the registry key fine, however I cannot write to the registry key even when I give full permissions to LOCAL SERVICE.

This is the code that throws the exception:

private void updateLocalRegistryVersion(Double newVersion)
{
    RegistryKey rk = Registry.LocalMachine;
    RegistryKey sk = rk.OpenSubKey(@"Software\CompanyName\Monitoring\Values");

    sk.SetValue("scriptversion", newVersion.ToString());
}

Any suggestions?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
dmck
  • 7,801
  • 7
  • 43
  • 79

1 Answers1

85

RegistryKey.OpenSubKey(string) does not open the key for writing. Try using the OpenSubKey(string, bool) overload to specify that you want the key to be writable.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Nicole Calinoiu
  • 20,843
  • 2
  • 44
  • 49