-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegEdit.cs
More file actions
183 lines (172 loc) · 8.28 KB
/
Copy pathRegEdit.cs
File metadata and controls
183 lines (172 loc) · 8.28 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System;
using Microsoft.Win32;
namespace Hopex.RegEdit
{
/// <summary>
/// Registry workflow.
/// </summary>
public class RegEdit
{
/// <summary>
/// Root registry key.
/// </summary>
private RegistryKey registryKey { get; } = Registry.CurrentUser;
/// <summary>
/// Returns the number of values in the section.
/// </summary>
/// <exception cref="System.Security.SecurityException"/>
/// <exception cref="ObjectDisposedException"/>
/// <exception cref="UnauthorizedAccessException"/>
/// <exception cref="System.IO.IOException"/>
public int ValueCount => registryKey.ValueCount;
/// <summary>
/// Returns the number of subsections for the current section.
/// </summary>
/// <exception cref="System.Security.SecurityException"/>
/// <exception cref="ObjectDisposedException"/>
/// <exception cref="UnauthorizedAccessException"/>
/// <exception cref="System.IO.IOException"/>
public int SubKeyCount => registryKey.SubKeyCount;
/// <summary>
/// Registry workflow.
/// </summary>
/// <param name="registryKey">Root registry key. By default HKEY_CURRENT_USER.</param>
public RegEdit(RegistryKey registryKey = null)
{
if (!registryKey.Equals(null))
{
this.registryKey = registryKey;
}
}
/// <summary>
/// Validating registry path.
/// </summary>
/// <param name="path">Registry path.</param>
/// <returns>Valid registry path.</returns>
/// <exception cref="ArgumentException"/>
/// <exception cref="ArgumentNullException"/>
private string PreparePath(string path) => path.Replace("/", "\\").Trim('\\');
/// <summary>
/// Writing data to the registry.
/// </summary>
/// <param name="path">The name or path of the subsection being created or opened. This line is case-insensitive.</param>
/// <param name="parameter">The name of the parameter to save.</param>
/// <param name="value">To save data.</param>
/// <param name="writable">A value of true indicates that the new subsection is writable; otherwise, the value is false.</param>
/// <param name="registryOptions">Registry parameter to use.</param>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="ArgumentException"/>
/// <exception cref="System.Security.SecurityException"/>
/// <exception cref="ObjectDisposedException"/>
/// <exception cref="UnauthorizedAccessException"/>
/// <exception cref="System.IO.IOException"/>
public void Write(string path, string parameter, object value, bool writable = true, RegistryOptions registryOptions = RegistryOptions.None)
{
registryKey
.CreateSubKey(
subkey: PreparePath(path),
writable: writable,
options: registryOptions
)
.SetValue(
name: parameter,
value: value
);
}
/// <summary>
/// Reading data from the registry.
/// </summary>
/// <param name="path">The name or path of the subsection being opened. This line is case-insensitive.</param>
/// <param name="parameter">The name of the parameter to read.</param>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="System.Security.SecurityException"/>
/// <exception cref="ObjectDisposedException"/>
/// <exception cref="UnauthorizedAccessException"/>
/// <exception cref="System.IO.IOException"/>
public object Read(string path, string parameter)
{
return registryKey
.OpenSubKey(name: PreparePath(path))
.GetValue(name: parameter);
}
/// <summary>
/// Deleting data from the registry.
/// </summary>
/// <param name="path">The name or path of the subsection being removed. This line is case-insensitive.</param>
/// <param name="parameter">The name of the parameter to remove.</param>
/// <param name="throwOnMissingValue">Indicates whether an exception should be thrown if the specified value cannot be found.
/// If this argument is true and the specified value does not exist, an exception occurs.
/// If this argument is false and the specified value does not exist, no actions are performed.
/// </param>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="ArgumentException"/>
/// <exception cref="System.Security.SecurityException"/>
/// <exception cref="ObjectDisposedException"/>
/// <exception cref="UnauthorizedAccessException"/>
/// <exception cref="System.IO.IOException"/>
public void DeleteValue(string path, string parameter, bool throwOnMissingValue = false)
{
registryKey
.OpenSubKey(name: PreparePath(path))
.DeleteValue(
name: parameter,
throwOnMissingValue: throwOnMissingValue
);
}
/// <summary>
/// Normal or recursive deletion of a registry key.
/// </summary>
/// <param name="path">The name or path of the subsection being removed. This line is case-insensitive.</param>
/// <param name="recurcive">Indicates the deletion of a subsection and all child subsections recursively.</param>
/// <param name="throwOnMissingSubKey">Specifies whether an exception should be thrown if the specified subsection cannot be found.
/// If this argument is true and the specified subsection does not exist, an exception occurs.
/// If this argument is false and the specified subsection does not exist, no actions are performed.
/// </param>
/// <exception cref="InvalidOperationException"/>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="ArgumentException"/>
/// <exception cref="System.Security.SecurityException"/>
/// <exception cref="ObjectDisposedException"/>
/// <exception cref="UnauthorizedAccessException"/>
/// <exception cref="System.IO.IOException"/>
public void DeleteKey(string path, bool recurcive = false, bool throwOnMissingSubKey = false)
{
if (recurcive)
{
registryKey.DeleteSubKeyTree(
subkey: path,
throwOnMissingSubKey: throwOnMissingSubKey
);
}
else
{
registryKey.DeleteSubKey(
subkey: path,
throwOnMissingSubKey: throwOnMissingSubKey
);
}
}
/// <summary>
/// Checks the specified registry key and returns the number of its values.
/// </summary>
/// <param name="path">The name or path of the subsection being checked. This line is case-insensitive.</param>
/// <returns>Number of values in the specified section.</returns>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="System.Security.SecurityException"/>
/// <exception cref="ObjectDisposedException"/>
/// <exception cref="UnauthorizedAccessException"/>
/// <exception cref="System.IO.IOException"/>
public int GetValueCount(string path) => registryKey.OpenSubKey(name: path).ValueCount;
/// <summary>
/// Checks the specified registry key and returns the number of its partitions.
/// </summary>
/// <param name="path">The name or path of the subsection being checked. This line is case-insensitive.</param>
/// <returns>Number of subsections for the specified section.</returns>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="System.Security.SecurityException"/>
/// <exception cref="ObjectDisposedException"/>
/// <exception cref="UnauthorizedAccessException"/>
/// <exception cref="System.IO.IOException"/>
public int GetSubKeyCount(string path) => registryKey.OpenSubKey(name: path).SubKeyCount;
}
}