我正在尝试创建易失性密钥,以系统管理员身份运行。
这是创建易失性密钥的代码
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)
答案 0 :(得分:0)
我发现了这个问题,
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);