注册表值未设置

时间:2017-04-20 18:24:57

标签: c# windows registry

我正在尝试使用我完成的一个小应用程序设置一些注册表值,但在运行我的程序后,值不会被编辑...

这是我的代码:

enum KeyLocation
{
    LocalMachine,
    CurrentUser,
}

private static void ChangeRegistry(KeyLocation location, string key, string keyName, RegistryValueKind type, object value)
{
    RegistryKey registryKey = null;

    try
    {
        if (location == KeyLocation.LocalMachine)
            registryKey = Registry.LocalMachine.OpenSubKey(key, true);

        else if (location == KeyLocation.CurrentUser)
            registryKey = Registry.CurrentUser.OpenSubKey(key, true);

        registryKey.SetValue(keyName, value, type);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    finally
    {
        if (registryKey != null)
            registryKey.Close();
    }
}

目前我正在尝试阻止Sticky键,所以我通过以下调用调用上面的方法:

string subkey1 = @"Control Panel\Accessibility\Keyboard Response";
string keyName1 = "Flags";
string newValue1 = "122";
ChangeRegistry(KeyLocation.CurrentUser, subkey1, keyName1, RegistryValueKind.String, newValue1);

我还会更改此链接上显示的其他值:https://answers.microsoft.com/en-us/windows/forum/windows_vista-desktop/i-cant-turn-off-sticky-keys/a7c9fc02-2d0f-4db6-89fb-e36eca3e2ac7

我执行我的应用程序,没有抛出任何异常,所以我重新启动计算机,但值没有改变...我正在使用管理员权限执行应用程序,提供正确的凭据。

在我的程序的其他部分,我创建并删除其他键,这工作正常......我做错了什么?或者至少,我该如何调试呢?

修改

我想我知道这个问题是什么......但我不知道如何解决它。

我在常规帐户中运行此应用程序,但是当我运行它时,我需要赋予它管理员权限,因此当应用程序运行时,编辑的密钥(来自Registry.CurrentUser)将在管理员配置文件中进行编辑,并且不是当前记录的用户)。这可能是问题吗?

0 个答案:

没有答案
相关问题