-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cs
95 lines (86 loc) · 2.48 KB
/
Config.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Reflection;
namespace Kawa.Configuration
{
/// <summary>
/// The base class for a Configuration that does not include any particular storage method.
/// </summary>
public abstract class Config
{
public abstract object GetValue(string name, object defaultVal);
public abstract void SetValue(string name, object value);
public void Load()
{
var myType = this.GetType();
var settings = myType.GetProperties();
foreach (var setting in settings)
{
var attribs = setting.GetCustomAttributes(true).OfType<SettingAttribute>().ToArray();
if (attribs.Length == 0)
continue;
var defaultVal = ((SettingAttribute)attribs[0]).Default;
var val = GetValue(setting.Name, defaultVal);
if (val == null)
val = defaultVal;
setting.SetValue(this, val, null);
}
}
public void Save()
{
var myType = this.GetType();
var settings = myType.GetProperties();
foreach (var setting in settings)
{
var attribs = setting.GetCustomAttributes(true).OfType<SettingAttribute>().ToArray();
if (attribs.Length == 0)
continue;
var val = setting.GetValue(this, null);
SetValue(setting.Name, val);
}
}
}
/// <summary>
/// A Kawa.Configuration that stores its values in the Windows Registry.
/// </summary>
public class RegistryConfig : Config
{
public string Path { get; set; }
public RegistryConfig() : base() { }
public RegistryConfig(string path)
{
Path = path;
Load();
}
public override object GetValue(string name, object defaultVal)
{
return Registry.GetValue(string.Format(@"HKEY_CURRENT_USER\SOFTWARE\{0}", Path), name, defaultVal);
}
public override void SetValue(string name, object value)
{
Registry.SetValue(string.Format(@"HKEY_CURRENT_USER\SOFTWARE\{0}", Path), name, value);
}
}
/// <summary>
/// Specifies that a field of the <see cref="Config"/> class should be stored.
/// </summary>
public class SettingAttribute : Attribute
{
/// <summary>
/// Gets a value indicating the default for this setting.
/// </summary>
public object Default { get; private set; }
/// <summary>
/// Initializes a new instance of the SettingAttribute class.
/// </summary>
/// <param name="defaultValue">the default value to return if the setting is not found in the Registry.</param>
public SettingAttribute(object defaultValue)
{
Default = defaultValue;
}
}
}