modifying the registry key value
Asked Answered
A

2

46

I have a registry path of the following

HKEY_LOCAL_MACHINE\SOFTWARE\COMPANY\COMPFOLDER

inside COMPFOLDER, I have a string value called "Deno" whose value is 0. I wish to change its value to 1 by code whenever I execute the code. Can anyone help me?

Accepted answered 11/1, 2012 at 8:19 Comment(3)
How did the value get in that registry key in the first place? I assume that you used the Microsoft.Win32.Registry class to write it, so you should use the same class to modify it. What problems did you have when you tried to do this?Holms
Use the Registry class as described here. msdn.microsoft.com/en-us/library/microsoft.win32.registry.aspxExhaustless
I'd assume that he navigated to the registry, copied the path and is desirous of creating a program to make changes to it through code.. Just a guess but that's how I got here. I appreciate your answer Cody Gray. It answered my question in part.Mier
S
68

It's been a while I did reg hacks, but something like this could work:

RegistryKey myKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Company\\Compfolder", true);
if(myKey != null)    {
   myKey.SetValue("Deno", "1", RegistryValueKind.String);
   myKey.Close();
}
Shon answered 11/1, 2012 at 8:24 Comment(2)
myKey should be in a using statement. See the remarks for RegistryKeyLawhorn
Since the key I had to modify started with HKCU, I replaced "Registry.LocalMachine." with "Registry.CurrentUser." and it worked all right. Thank you!Morphine
M
24
using (RegistryKey key = regKeyRoot.OpenSubKey(KeyName, true)) // Must dispose key or use "using" keyword
{
    if (key != null)  // Must check for null key
    {
        key.SetValue(attribute, value);
    }
}
Manas answered 4/6, 2014 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.