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?

3
  • 4
    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? Commented Jan 11, 2012 at 8:21
  • Use the Registry class as described here. msdn.microsoft.com/en-us/library/microsoft.win32.registry.aspx Commented Jan 11, 2012 at 8:23
  • 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. Commented Jan 15, 2015 at 7:22

2 Answers 2

68

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

using RegistryKey myKey = Registry.LocalMachine.OpenSubKey(
    @"SOFTWARE\Company\Compfolder", true);
if (myKey != null)    {
    myKey.SetValue("Deno", "1", RegistryValueKind.String);
    myKey.Close();
}
Sign up to request clarification or add additional context in comments.

2 Comments

myKey should be in a using statement. See the remarks for RegistryKey
Since the key I had to modify started with HKCU, I replaced "Registry.LocalMachine." with "Registry.CurrentUser." and it worked all right. Thank you!
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);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.