Cannot write to Registry Key, getting UnauthorizedAccessException

24,345

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.

Share:
24,345
dmck
Author by

dmck

Working with and interested in the following: Microsoft .NET (C#) Android Windows Mobile (.NET Compact Framework 2.0 / 3.5) HTML5 / JavaScript / JQuery Functional Programming (LINQ / F#) Reactive Extensions (Rx) PowerShell

Updated on July 27, 2020

Comments

  • dmck
    dmck almost 4 years

    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?

  • Mark Carpenter
    Mark Carpenter about 13 years
    +1 Thank you! This is an annoying (yet blatantly obvious) overload. Kind of embarrassed I missed that...
  • Nandun
    Nandun about 3 years
    always look at the overloads lol.. Thank you for this!!