Skip to content

Instantly share code, notes, and snippets.

@mrstebo
Created June 2, 2020 11:16
Show Gist options
  • Select an option

  • Save mrstebo/4d5d3e49b2bba4f682ce539aa9df65a9 to your computer and use it in GitHub Desktop.

Select an option

Save mrstebo/4d5d3e49b2bba4f682ce539aa9df65a9 to your computer and use it in GitHub Desktop.
c-net-registry-based-configuration-base-class-4
protected T GetValue<T>(string key, T defaultValue)
{
try
{
using (var rk = GetRegistryKey())
{
using (var sk = rk.OpenSubKey(RootKey))
{
object o = null;
if (sk == null || (o = sk.GetValue(key, null)) == null)
SetValue(key, (o = defaultValue));
return (T)o;
}
}
}
catch (Exception ex)
{
Trace.TraceError("Failed to get value for {0}: {1}", key, ex);
throw;
}
}
protected void SetValue<T>(string key, T value)
{
try
{
using (var rk = GetRegistryKey())
{
using (var sk = rk.CreateSubKey(RootKey))
{
sk.SetValue(key, value);
}
}
}
catch (Exception ex)
{
Trace.TraceError("Failed to set value for {0}: {1}", key, ex);
throw;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment