Search code examples
c#ioexceptionregistrykey

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


I am trying to create volatile key, running as system administrator.

here is code that creates volatile key

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

I am getting System.IO.IOException: The parameter is incorrect

on API saw this documentation, but not sure what I need to do to resolve this.

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

here is stack trace

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)

Solution

  • I found the issue,

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

    should be

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