DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Open/Creates A Base Key In The Registry

09.11.2007
| 3714 views |
  • submit to reddit
        // open a Base Key in the registry

       public static RegistryKey GetKey(string baseKey)
        {
            RegistryKey key;
            try
            {
                key = Registry.LocalMachine.OpenSubKey(baseKey, true);

                if (key == null)
                {
                    key = Registry.LocalMachine.CreateSubKey(baseKey);
                }
                else
                {
                    MessageBox.Show("Base key resolved");
                }
            }
            catch (Exception e)
            {
                return null;
            }
            return key;
        }