-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistryHelper.cs
More file actions
57 lines (49 loc) · 2.12 KB
/
RegistryHelper.cs
File metadata and controls
57 lines (49 loc) · 2.12 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
using Microsoft.Win32;
namespace ReWindows
{
public static class RegistryHelper
{
public static void SetDword(string path, string keyName, int value)
{
string root = path.Split('\\')[0];
string subKey = path[(root.Length + 1)..];
RegistryKey baseKey = root == "HKEY_LOCAL_MACHINE" ? Registry.LocalMachine : Registry.CurrentUser;
using var key = baseKey.CreateSubKey(subKey, true);
key?.SetValue(keyName, value, RegistryValueKind.DWord);
}
public static int GetDword(string path, string keyName, int defaultValue = 0)
{
try
{
string root = path.Split('\\')[0];
string subKey = path[(root.Length + 1)..];
RegistryKey baseKey = root == "HKEY_LOCAL_MACHINE" ? Registry.LocalMachine : Registry.CurrentUser;
using var key = baseKey.OpenSubKey(subKey);
var val = key?.GetValue(keyName);
return val is int i ? i : defaultValue;
}
catch { return defaultValue; }
}
public static void SetString(string path, string keyName, string value)
{
string root = path.Split('\\')[0];
string subKey = path[(root.Length + 1)..];
RegistryKey baseKey = root == "HKEY_LOCAL_MACHINE" ? Registry.LocalMachine : Registry.CurrentUser;
using var key = baseKey.CreateSubKey(subKey, true);
key?.SetValue(keyName, value, RegistryValueKind.String);
}
public static string GetString(string path, string keyName, string defaultValue = "")
{
try
{
string root = path.Split('\\')[0];
string subKey = path[(root.Length + 1)..];
RegistryKey baseKey = root == "HKEY_LOCAL_MACHINE" ? Registry.LocalMachine : Registry.CurrentUser;
using var key = baseKey.OpenSubKey(subKey);
var val = key?.GetValue(keyName);
return val is string s ? s : defaultValue;
}
catch { return defaultValue; }
}
}
}