简体   繁体   English

C#写入HKCU中的注册表项不起作用

[英]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: 我正在尝试使用以下代码在HKCU \\ SOFTWARE \\ Classes \\ CLSID中创建一个键:

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. 它不会添加条目,它不会引发错误,并且ProcessMonitor中不会出现任何内容。

This is true running visual studio as Administrator, but also running as a regular user. 这是以管理员身份运行visual studio,但也以普通用户身份运行。

Any ideas? 有任何想法吗?

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. 由AlexK提供,答案是由于64位注册表重定向而写入了条目 - 条目被写入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: 我发现在64位Windows安装上针对标准节点的解决方案是使用RegistryKey.OpenBaseKey,如下所示:

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

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 https://www.codeproject.com/Questions/273588/How-to-create-a-guid-key-under-HKEY-CLASSES-ROOT-C

Edit 1: 编辑1:

Another example :- 另一个例子 :-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM