Search code examples
c#registry

C# writing to Registry key in HKCU doesn't work


I am attempting to create a key in the HKCU\SOFTWARE\Classes\CLSID using the following code:

var softwareKey = Registry.CurrentUser;            
var key = softwareKey?.OpenSubKey("SOFTWARE\\Classes\\CLSID", true);
key = key?.CreateSubKey("{220176f5-8cff-4e42-b20c-c2d6b32b133c}", RegistryKeyPermissionCheck.ReadWriteSubTree);            
key?.SetValue("", "test value");

It doesn't add the entry, it doesn't raise an error and nothing whatsoever appears in ProcessMonitor.

This is true running visual studio as Administrator, but also running as a regular user.

Any ideas?


Solution

  • Courtesy of AlexK, the answer to this was that the entry was being written due to 64 bit registry redirection - the entries were being written to HKEY_CURRENT_USER\SOFTWARE\Classes\WOW6432Node\CLSID.

    And I have found the solution to target the standard node on a 64-bit windows installation is to use RegistryKey.OpenBaseKey as follows:

    var softwareKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);