Thursday, May 13, 2010

Some Registry Methods

Here are a couple of helper methods to work with Windows Registry:
The first method makes sure you get a reference to a registry subkey. If it does not exist it creates one first. The second one is a preferred way to read a registry value.



 public static RegistryKey GetKey(string RegKey)
 {
     RegistryKey Key = Registry.CurrentUser.OpenSubKey(RegKey, true);

     if (Key == null)
     {
         Key = Registry.CurrentUser.CreateSubKey(RegKey);
     }
     return Key;
}

public static string ReadRegistryValue(RegistryKey key, string regValName)
{
     string[] values = key.GetValueNames();
     if (values.Contains(regValName))
          return key.GetValue(regValName).ToString();
     else
          return String.Empty;
}

No comments:

Post a Comment