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?