0

I am trying to set some Registry values with a small application I done, but after running my program values are not edited...

This is my code:

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

Currently I am trying to block the Sticky keys, so I am calling the above method with the following call:

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

I also change other values, indicated on this link: https://answers.microsoft.com/en-us/windows/forum/windows_vista-desktop/i-cant-turn-off-sticky-keys/a7c9fc02-2d0f-4db6-89fb-e36eca3e2ac7

I execute my application, no exception are thrown, so I restart my computer, but the values did not change... I am executing the application with admin rights, providing the correct credentials.

In other part of my program I create and delete other keys, and that works fine... What I am doing wrong? Or at least, how can I debug this?

EDIT:

I think I know whats the problem... But I don't know how to solve it.

I am running this application in a regular account, but when I run it I need to give it administrator rights, so when the application runs the keys edited (from Registry.CurrentUser, are edited in the admin profile, and not the current logged user). Can this be the problem?

lulas
  • 860
  • 9
  • 29
  • 1
    This code is working fine for me. I don't have to reboot, just have refresh the Registry Editor (View -> Refresh) and it shows the new value fine. How are you checking the value? – Quantic Apr 20 '17 at 18:36
  • @Quantic After checking your comment, I changed my code to before and after the edit print the value of the key, and I found out that the keys where already updated, could you check my edit and confirm if that can be the problem? – lulas Apr 20 '17 at 19:08
  • Your edit makes sense, in which case the answer is probably [this](http://stackoverflow.com/questions/6564153/edit-registry-key-of-other-user). – Quantic Apr 20 '17 at 19:14
  • Sorry for the delay, I solved my problem, it was related with the user, when I runned the app as admin the user account would change to the admin account, so the changes done to the Key Current User would happen in the admin, thats why I didn't saw the changes on my current user. I tried to use the link you gave to solve this, but the impersonation didn't solve my problem, so I followed a simplier approach, I detected the SID of the current user (by detecting the owner of explorer.exe), and then I changed the Keys in the User with that SID in the Key USERS. Thank you for the input @Quantic. – lulas Apr 21 '17 at 20:34

0 Answers0