Skip to content

Instantly share code, notes, and snippets.

@yemrekeskin
Created October 1, 2013 13:31
Show Gist options
  • Save yemrekeskin/6778481 to your computer and use it in GitHub Desktop.
Save yemrekeskin/6778481 to your computer and use it in GitHub Desktop.
public static class RegistryConfigurator
{
private static RegistryKey baseRegistryKey = Registry.LocalMachine;
public static RegistryKey BaseRegistryKey
{
get { return baseRegistryKey; }
set { baseRegistryKey = value; }
}
private static string subKey = "SOFTWARE\\My-Application";
public static string SubKey
{
get { return subKey; }
set { subKey = value; }
}
public static string ReadKey(string Key)
{
ThrowError(Key);
RegistryKey rgstryKey = baseRegistryKey.OpenSubKey(subKey);
return rgstryKey == null ? null : (string)rgstryKey.GetValue(Key);
}
public static bool DeleteKey(string Key)
{
ThrowError(Key);
RegistryKey rgstryKey = baseRegistryKey.CreateSubKey(subKey);
if (rgstryKey == null)
return false;
else
rgstryKey.DeleteValue(Key);
return true;
}
public static bool Write(string Key, object Value)
{
ThrowError(Key);
try
{
RegistryKey rgstryKey = baseRegistryKey.CreateSubKey(subKey);
rgstryKey.SetValue(Key.ToUpper(), Value);
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
return true;
}
public static bool DeleteSubKeyTree()
{
try
{
RegistryKey rgstryKey = baseRegistryKey.OpenSubKey(subKey);
if (rgstryKey != null)
baseRegistryKey.DeleteSubKeyTree(subKey);
return true;
}
catch (Exception ex)
{
return false;
}
}
private static void ThrowError(string value)
{
if (String.IsNullOrEmpty(value))
throw new ApplicationException("Your Key is Null...");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment