Usually an application needs to save or retrieve data from windows registry. For this reason a registry manager class is required. The following code snippet provides a simple yet effective way for handling windows registry keys.

public class Manager
{
   public void addRegistry(string subkey, string name, string value)
   {
      if (Microsoft.Win32.Registry.GetValue(@""+subkey, name, null) == null)
      { 
         Microsoft.Win32.RegistryKey key;
         key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(subkey);
         key.SetValue(name, value);
         key.Close();
      }
   }

   public void removeRegistry(string keyName)
   {
      using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@""keyName, true))
      {
         if (key != null)
         key.DeleteValue("MyApp");
      }
   }

   public bool existsRegistry(string subkey, string name)
   {
      return Microsoft.Win32.Registry.GetValue(@"" + subkey, name, null) != null ? true : false;
   } 

   public string getRegistryValue(string subkey, string name)
   {
      return Microsoft.Win32.Registry.GetValue(@"" + subkey, name, null).ToString();
   }
}

 

Author

Write A Comment