Search code examples
c#registry

c# Registry Unable to Create a SubKey


I couldn't create a subkey under the following Path :

Registry.CurrentUser + @"\Software\";

I tried that using the following code :

       RegistryKey Key = Registry.CurrentUser.OpenSubKey("SOFTWARE").CreateSubKey("MyKey");
        Key.SetValue("Check","Yes");

Cannot write : Cannot write to the registry key.

I also tried to open the application with the admin privileges , but it still display the same error.

Please note that i can create subkeys in Registry.CurrentUser without admin privileges ,And I Have the Permission Read/Full Control on the Both Paths (Current User and SOFTWARE)


Solution

  • It's because according to MSDN, OpenSubKey retrieves a subkey as read-only. Instead, try to use OpenSubKey(name, true). In this code, true means I need write access to the key. For more information check this MSDN link out.