Skip to content

Instantly share code, notes, and snippets.

View mrstebo's full-sized avatar
💻

Steven Atkinson mrstebo

💻
View GitHub Profile
@mrstebo
mrstebo / TestConfig.cs
Created June 2, 2020 11:36
deriving-from-our-registryconfig-class-2
public string TestSetting1
{
get
{
return GetValue(ParsePropertyMethodName(MethodBase.GetCurrentMethod().Name), string.Empty);
}
set
{
SetValue(ParsePropertyMethodName(MethodBase.GetCurrentMethod().Name), value);
}
@mrstebo
mrstebo / TestConfig.cs
Created June 2, 2020 11:35
deriving-from-our-registryconfig-class-1
public sealed class TestConfig : RegistryConfig
{
public TestConfig()
: base(RegistryHive.CurrentUser, @"SOFTWARE\Test")
{
}
}
@mrstebo
mrstebo / RegistryConfig.cs
Created June 2, 2020 11:26
extending-the-c-registry-based-configuration-class-3
protected static string ParsePropertyMethodName(string methodName)
{
if (methodName.StartsWith("get_") || methodName.StartsWith("set_"))
methodName = methodName.Substring(4);
return methodName;
}
@mrstebo
mrstebo / RegistryConfig.cs
Created June 2, 2020 11:25
extending-the-c-registry-based-configuration-class-2
public void Initialize()
{
foreach (var propInfo in GetProperties())
{
try
{
if (propInfo != null)
{
var o = propInfo.GetValue(this, null);
}
@mrstebo
mrstebo / RegistryConfig.cs
Created June 2, 2020 11:24
extending-the-c-registry-based-configuration-class-1
protected virtual IEnumerable<PropertyInfo> GetProperties()
{
return GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.GetProperty);
}
@mrstebo
mrstebo / RegistryConfig.cs
Created June 2, 2020 11:18
c-net-registry-based-configuration-base-class-6
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
namespace ns_stebo
{
public abstract class RegistryConfig
{
@mrstebo
mrstebo / RegistryConfig.cs
Created June 2, 2020 11:17
c-net-registry-based-configuration-base-class-5
public bool Exists(string key)
{
return GetValue<object>(key, null) != null;
}
@mrstebo
mrstebo / RegistryConfig.cs
Created June 2, 2020 11:16
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)
@mrstebo
mrstebo / RegistryConfig.cs
Created June 2, 2020 11:15
c-net-registry-based-configuration-base-class-3
protected RegistryKey GetRegistryKey()
{
switch (Hive)
{
case RegistryHive.ClassesRoot:
return Registry.ClassesRoot;
case RegistryHive.CurrentConfig:
return Registry.CurrentConfig;
@mrstebo
mrstebo / RegistryConfig.cs
Created June 2, 2020 11:14
c-net-registry-based-configuration-base-class-2
namespace ns_stebo
{
public abstract class RegistryConfig
{
public RegistryHive Hive
{
get;
private set;
}