无法更改注册表变量C#

时间:2016-12-21 21:56:18

标签: c# visual-studio

我正在使用C#创建自己的自助服务终端,为此我将杀死资源管理器(Windows shell),我不需要它来自动重启。为此我需要在注册表中更改一个变量,但是当我尝试从IDE运行它时,它会给我一个错误:

System.UnauthorizedAccessException:拒绝访问注册表项%KEYPATH%。

如果我尝试以管理员身份运行可执行文件,则不会收到任何错误消息但没有任何更改,变量仍设置为“1”。 我还在注册表中为当前用户甚至每个人编辑了该分支的权限,从而在注册表中提供对该分支的完全访问权限。这是我的C#代码

RegistryKey explorerAutoRestart;
string ear_keypath = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon";

//GETTING ACCESS ERROR HERE
explorerAutoRestart = Registry.LocalMachine.CreateSubKey(ear_keypath); 
switch (status)
{
    case "0":
        RegistryKey key = Registry.LocalMachine.CreateSubKey(@"Software\Microsoft\Windows NT\CurrentVersion\Winlogon");
        key.SetValue("AutoRestartShell", 0x00000000, RegistryValueKind.DWord);
        key.Close();
        break;

    case "1":
        explorerAutoRestart.SetValue("AutoRestartShell", 1, RegistryValueKind.DWord);
        explorerAutoRestart.Close();
        break;
}

我想知道是什么导致了这个问题,我该如何更改它的值 AutoRestartShell - REG_DWORD

将CreateSUbKey更改为OpenSubKey(path,true)仅将错误更改为  不允许请求注册表访问。

也许Windows不允许对该分支进行外部编辑?

1 个答案:

答案 0 :(得分:2)

管理解决此问题。显然,我的密钥被WOW64重定向,并且某种程度上没有给我注册分支的许可? 尽管如此,代码保存了我:

RegistryKey localMachine = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
            RegistryKey regKey = localMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", true);
                    regKey.SetValue("Shell", "bat.exe", RegistryValueKind.String);
                    regKey.Close();

Changing the Shell registry