1 | using Microsoft.Win32;
|
---|
2 |
|
---|
3 | namespace Common
|
---|
4 | {
|
---|
5 | public static class RegistryExtension
|
---|
6 | {
|
---|
7 | public static bool GetValueBool(this RegistryKey regKey, string name, bool defaultValue)
|
---|
8 | {
|
---|
9 | object value = regKey.GetValue(name, defaultValue);
|
---|
10 | if (value is string)
|
---|
11 | {
|
---|
12 | if ((string)value == "True") return true;
|
---|
13 | else return false;
|
---|
14 | }
|
---|
15 | else return defaultValue;
|
---|
16 | }
|
---|
17 |
|
---|
18 | public static bool? GetValueBoolNullable(this RegistryKey regKey, string name, bool? defaultValue)
|
---|
19 | {
|
---|
20 | object value = regKey.GetValue(name, defaultValue);
|
---|
21 | if (value is string)
|
---|
22 | {
|
---|
23 | if ((string)value == "False") return false;
|
---|
24 | else if ((string)value == "True") return true;
|
---|
25 | else return null;
|
---|
26 | }
|
---|
27 | else return defaultValue;
|
---|
28 | }
|
---|
29 |
|
---|
30 | public static string GetValueString(this RegistryKey regKey, string name, string defaultValue)
|
---|
31 | {
|
---|
32 | object value = regKey.GetValue(name, defaultValue);
|
---|
33 | if (value is string) return (string)value;
|
---|
34 | else return defaultValue;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public static int GetValueInt(this RegistryKey regKey, string name, int defaultValue)
|
---|
38 | {
|
---|
39 | object value = regKey.GetValue(name, defaultValue);
|
---|
40 | if (value is int) return (int)value;
|
---|
41 | else return defaultValue;
|
---|
42 | }
|
---|
43 |
|
---|
44 | public static int? GetValueIntNullable(this RegistryKey regKey, string name, int? defaultValue)
|
---|
45 | {
|
---|
46 | object value = regKey.GetValue(name, defaultValue);
|
---|
47 | if (value is int) return (int)value;
|
---|
48 | else if (value is string && (string)value == "") return null;
|
---|
49 | else return defaultValue;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public static long GetValueLong(this RegistryKey regKey, string name, long defaultValue)
|
---|
53 | {
|
---|
54 | object value = regKey.GetValue(name, defaultValue);
|
---|
55 | if (value is string && long.TryParse(value.ToString(), out long longValue)) return longValue;
|
---|
56 | else return defaultValue;
|
---|
57 | }
|
---|
58 |
|
---|
59 | public static float GetValueFloat(this RegistryKey regKey, string name, float defaultValue)
|
---|
60 | {
|
---|
61 | object value = regKey.GetValue(name, defaultValue.ToString());
|
---|
62 | if (float.TryParse((string) value, out float num))
|
---|
63 | {
|
---|
64 | return num;
|
---|
65 | }
|
---|
66 | else return defaultValue;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public static void SetValueBool(this RegistryKey regKey, string name, bool value)
|
---|
70 | {
|
---|
71 | if (value) regKey.SetValue(name, "True");
|
---|
72 | else regKey.SetValue(name, "False");
|
---|
73 | }
|
---|
74 |
|
---|
75 | public static void SetValueBoolNullable(this RegistryKey regKey, string name, bool? value)
|
---|
76 | {
|
---|
77 | if (value != null)
|
---|
78 | {
|
---|
79 | if (value.Value) regKey.SetValue(name, "True");
|
---|
80 | else regKey.SetValue(name, "False");
|
---|
81 | } else regKey.SetValue(name, "");
|
---|
82 | }
|
---|
83 |
|
---|
84 | public static void SetValueString(this RegistryKey regKey, string name, string value)
|
---|
85 | {
|
---|
86 | if (value == null) value = "";
|
---|
87 | regKey.SetValue(name, value);
|
---|
88 | }
|
---|
89 |
|
---|
90 | public static void SetValueInt(this RegistryKey regKey, string name, int value)
|
---|
91 | {
|
---|
92 | regKey.SetValue(name, value);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public static void SetValueIntNullable(this RegistryKey regKey, string name, int? value)
|
---|
96 | {
|
---|
97 | if (value != null)
|
---|
98 | {
|
---|
99 | regKey.SetValue(name, value.Value);
|
---|
100 | } else regKey.SetValue(name, "");
|
---|
101 | }
|
---|
102 |
|
---|
103 | public static void SetValueLong(this RegistryKey regKey, string name, long value)
|
---|
104 | {
|
---|
105 | regKey.SetValue(name, value);
|
---|
106 | }
|
---|
107 |
|
---|
108 | public static void SetValueFloat(this RegistryKey regKey, string name, float value)
|
---|
109 | {
|
---|
110 | regKey.SetValue(name, value);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | } |
---|