Reading and Writing Registry Values via C# Code

Good Day -

I am trying to find some .NET 2.0 methods for reading and writing Registry values. Do such methods exist or is there some way to home-grow this?

Thanks
HubertFarnsworthAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Try something like this:


using Microsoft.Win32;
 
public enum RegistryKeyRoot
{
    LocalMachine,
    Users,
    CurrentUser,
}
 
public class WindowsRegistry
{
 
    public static object GetValue(RegistryKeyRoot root, string key, string name)
    {
        RegistryKey subKey = FindSubKey(root, key, false);
 
        if (subKey != null)
            return subKey.GetValue(name);
 
        return null;
    }
 
    public static void SetValue(RegistryKeyRoot root, string key, string name, object value)
    {
        RegistryKey subKey = FindSubKey(root, key, true);
 
        if (subKey != null)
            subKey.SetValue(name, value);
    }
 
    private static RegistryKey FindSubKey(RegistryKeyRoot root, string keyName, bool writable)
    {
        RegistryKey key = null;
        switch (root)
        {
            case RegistryKeyRoot.LocalMachine:
                key = Registry.LocalMachine;
                break;
            case RegistryKeyRoot.Users:
                key = Registry.Users;
                break;
            case RegistryKeyRoot.CurrentUser:
                key = Registry.CurrentUser;
                break;
        }
 
        RegistryKey subKey = key.OpenSubKey(keyName, writable);
 
        return subKey;
    }
 
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedCommented:
Example usage:

bool isFullScreen = (Convert.ToInt32((WindowsRegistry.GetValue(RegistryKeyRoot.CurrentUser, @"Software\Microsoft\VisualStudio\8.0", "FullScreen"))) == 1);

Bob
0
HubertFarnsworthAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.