James Harcourt
James Harcourt

Reputation: 6379

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?

Upvotes: 2

Views: 2021

Answers (2)

James Harcourt
James Harcourt

Reputation: 6379

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);

Upvotes: 4

Anand Systematix
Anand Systematix

Reputation: 632

You can refer below example :-

RegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Classes\WOW6432Node\CLSID\", True)
RegKey.CreateSubKey("{00000000-EAF8-3196-9360-1AADDCDABE1B}")
RegKey.Close()

Reference link :-

https://www.codeproject.com/Questions/273588/How-to-create-a-guid-key-under-HKEY-CLASSES-ROOT-C

Edit 1:

Another example :-

Microsoft.Win32.RegistryKey key;  
key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Names");  
key.SetValue("Name", "Isabella");  
key.Close();  

Upvotes: 0

Related Questions