System.IO.IOException: Microsoft 的参数不正确。Win32.RegistryKey.Win32Error(Int32 errorCode, String str)

System.IO.IOException: The parameter is incorrect at Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode, String str)

我正在尝试创建可变密钥,运行 作为系统管理员。

这是创建可变密钥的代码

 internal static void SetVolitileVmRestartKey()
            {
                Registry.LocalMachine.CreateSubKey(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MKeys\Restart", RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
                Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MKeys\Restart",
                    "VmRestartRequestedTime",
                    DateTime.UtcNow.ToString("o", CultureInfo.DefaultThreadCurrentCulture));
            }

我正在 System.IO.IOException: 参数不正确

在 API 上看到了这个文档,但不确定我需要做什么来解决这个问题。

//   T:System.IO.IOException:
//     The nesting level exceeds 510.-or-A system error occurred, such as deletion of
//     the key or an attempt to create a key in the Microsoft.Win32.Registry.LocalMachine
//     root.
 [ComVisible(false)]
        public RegistryKey CreateSubKey(string subkey, RegistryKeyPermissionCheck permissionCheck, RegistryOptions options);

这里是堆栈跟踪

System.IO.IOException: The parameter is incorrect.

   at Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode, String str)
   at Microsoft.Win32.RegistryKey.CreateSubKeyInternal(String subkey, RegistryKeyPermissionCheck permissionCheck, Object registrySecurityObj, RegistryOptions registryOptions)
   at Microsoft.Win32.RegistryKey.CreateSubKey(String subkey, RegistryKeyPermissionCheck permissionCheck, RegistryOptions options)

我发现了问题,

Registry.LocalMachine.CreateSubKey(@"**HKEY_LOCAL_MACHINE**\SOFTWARE\Microsoft\MKeys\Restart", RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);

应该是

Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\MKeys\Restart", RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);