-
Notifications
You must be signed in to change notification settings - Fork 61
/
RegistryHelper.cs
224 lines (210 loc) · 7.92 KB
/
RegistryHelper.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using Microsoft.Win32;
using System;
using System.Windows.Forms;
namespace WHC.OrderWater.Commons
{
/// <summary>
/// 注册表操作辅助类
/// </summary>
public sealed class RegistryHelper
{
#region 基础操作(读取和保存)
private static string Software_Key = @"Software\DeepLand\OrderWater";
/// <summary>
/// Gets the value by registry key. If the key does not exist, return empty string.
/// </summary>
/// <param name="key">registry key</param>
/// <returns>Returns the value of the specified key.</returns>
///
private static string ArrayToString(string[] strings)
{
string result = string.Empty;
foreach (var item in strings)
{
result = result + item + ",";
}
return result.Substring(0, result.Length - 1);
}
private static (RegistryKey, string) FormatBaseKey(string fullSoftwareKey)
{
RegistryKey registryKey = Registry.LocalMachine;
int indexOfFirstBackslash = fullSoftwareKey.IndexOf('\\');
string baseKey = fullSoftwareKey.Substring(0, indexOfFirstBackslash);
string subKey = fullSoftwareKey.Substring(indexOfFirstBackslash + 1);
if (baseKey.Contains("USER"))
{
registryKey = Registry.CurrentUser;
}
if (baseKey.Contains("ROOT"))
{
registryKey = Registry.ClassesRoot;
}
if (baseKey.Contains("USERS"))
{
registryKey = Registry.Users;
}
if (baseKey.Contains("CONFIG"))
{
registryKey = Registry.CurrentConfig;
}
return (registryKey, subKey);
}
public static string GetValue(string softwareKey, string key)
{
(RegistryKey registryKey, string subKey) = FormatBaseKey(softwareKey);
return GetValue(registryKey, subKey, key);
}
/// <summary>
/// Gets the value by registry key. If the key does not exist, return empty string.
/// </summary>
/// <param name="key">registry key</param>
/// <returns>Returns the value of the specified key.</returns>
public static string GetValue(RegistryKey registryKey, string softwareKey, string key)
{
const string parameter = "key";
if (null == key)
{
throw new ArgumentNullException(parameter);
}
string strRet = string.Empty;
try
{
RegistryKey regKey = registryKey.OpenSubKey(softwareKey);
object value = regKey.GetValue(key);
if (value == null)
{
return "未设置";
}
if (value is string[] stringArray)
{
if (stringArray.Length.Equals(0))
{
return "";
}
strRet = ArrayToString((string[])value);
return strRet;
}
strRet = regKey.GetValue(key).ToString();
}
catch
{
return strRet;
}
return strRet;
}
/// <summary>
/// Saves the key and the value to registry.
/// </summary>
/// <param name="key">registry key</param>
/// <param name="value">the value of the key</param>
/// <returns>Returns true if successful, otherwise return false.</returns>
public static void SaveValue(string softwareKey, string key, string value)
{
(RegistryKey registryKey, string subKey) = FormatBaseKey(softwareKey);
SaveValue(registryKey, subKey, key, value, "String");
}
public static void SaveValue(string softwareKey, string key, string value, string kind)
{
(RegistryKey registryKey, string subKey) = FormatBaseKey(softwareKey);
SaveValue(registryKey, subKey, key, value, kind);
}
/// <summary>
/// Saves the key and the value to registry.
/// </summary>
/// <param name="key">registry key</param>
/// <param name="value">the value of the key</param>
/// <returns>Returns true if successful, otherwise return false.</returns>
public static void SaveValue(RegistryKey registryKey, string softwareKey, string key, string value, string kind)
{
try
{
const string parameter1 = "key";
const string parameter2 = "value";
if (null == key)
{
throw new ArgumentNullException(parameter1);
}
if (null == value)
{
throw new ArgumentNullException(parameter2);
}
RegistryKey reg;
reg = registryKey.OpenSubKey(softwareKey, true);
if (null == reg)
{
reg = registryKey.CreateSubKey(softwareKey);
}
(RegistryValueKind valueType, object valueReality) = GetValueKind(kind, value);
reg.SetValue(key, valueReality, valueType);
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 获取配置文件检测项数据类型
/// </summary>
/// <param name="kind">检测项类型</param>
/// <param name="valueType">转换后的注册表数据类型</param>
/// <returns></returns>
private static (RegistryValueKind, object) GetValueKind(string kind, string value)
{
RegistryValueKind valueType;
switch (kind)
{
case "string":
valueType = RegistryValueKind.String;
return (valueType, value);
case "expandstring":
valueType = RegistryValueKind.ExpandString;
return (valueType, value);
/*case "Binary":
valueType = RegistryValueKind.Binary;
break;*/
case "dword":
valueType = RegistryValueKind.DWord;
return (valueType, value);
case "qword":
valueType = RegistryValueKind.QWord;
return (valueType, value);
case "multistring":
valueType = RegistryValueKind.MultiString;
return (valueType, value.Split(','));
default:
valueType = RegistryValueKind.String;
return (valueType, value);
}
}
#endregion
#region 自动启动程序设置
public static bool CheckStartWithWindows()
{
RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
if (regkey != null && (string)regkey.GetValue(Application.ProductName, "null", RegistryValueOptions.None) != "null")
{
Registry.CurrentUser.Flush();
return true;
}
Registry.CurrentUser.Flush();
return false;
}
public static void SetStartWithWindows(bool startWin)
{
RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
if (regkey != null)
{
if (startWin)
{
regkey.SetValue(Application.ProductName, Application.ExecutablePath, RegistryValueKind.String);
}
else
{
regkey.DeleteValue(Application.ProductName, false);
}
Registry.CurrentUser.Flush();
}
}
#endregion
}
}