c# Registry 无法创建子项

c# Registry Unable to Create a SubKey

我无法在以下路径下创建子项:

Registry.CurrentUser + @"\Software\";

我尝试使用以下代码:

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

无法写入:Cannot write to the registry key.

我也试过用管理员权限打开应用,还是报同样的错误。

请注意,我可以在 Registry.CurrentUser 中创建子项而无需管理员权限,并且我有权 Read/Full 控制两个路径(当前用户和软件)

这是因为根据 MSDN,OpenSubKey 以只读方式检索子项。相反,请尝试使用 OpenSubKey(name, true)。在此代码中,true 表示我需要对密钥的写入权限。有关详细信息,请查看此 MSDN link