-
Notifications
You must be signed in to change notification settings - Fork 0
/
Registry.cs
72 lines (67 loc) · 1.67 KB
/
Registry.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
using System;
using Microsoft.Win32;
namespace DiskFill
{
public class WindowsRegistry
{
private readonly RegistryKey regKey;
private RegistryKey subKey;
private readonly bool AutoCreateMissing = true;
public WindowsRegistry(RegistryHive hive)
{
regKey = RegistryKey.OpenRemoteBaseKey( hive,"" );
}
/// <summary>
///
/// </summary>
/// <param name="subKeyName"></param>
public void OpenSubKey( string subKeyName )
{
if (AutoCreateMissing)
subKey = regKey.CreateSubKey(subKeyName);
else
subKey = regKey.OpenSubKey( subKeyName );
}
/// <summary>
/// Reads a value from the registry
/// </summary>
/// <param name="Name"></param>
/// <returns></returns>
public object GetValue( string Name )
{
object Value = subKey.GetValue( Name );
if ( Value==null )
throw new ArgumentException("Missing value for registry key " + ToString() +
@"\" + Name + ", and no default value supplied!");
return Value;
}
/// <summary>
/// Reads a value from the registry
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns>Value stored in registry, DefaultValue if not found.</returns>
public object GetValue( string name, object defaultValue )
{
object value = subKey.GetValue( name);
if (value == null)
{
if (AutoCreateMissing)
subKey.SetValue( name, defaultValue );
return defaultValue;
}
return value;
}
/// <summary>
/// Write a value to the registry
/// </summary>
public void SetValue( string name, object data )
{
subKey.SetValue( name, data );
}
public override string ToString()
{
return regKey.ToString();
}
}
}