代码改变世界

c# 读写注册表

2019-05-16 23:09  hbren  阅读(4193)  评论(0编辑  收藏  举报

c# 简单读写注册表

 1 /// <summary>
 2 /// 注册表
 3 /// </summary>
 4 public class Regedit
 5 {
 6     private const string SUB_KEY = "SOFTWARE";
 7     private static readonly RegistryKey HKML;
 8     private static readonly RegistryKey SOFTWARE;
 9 
10     static Regedit()
11     {
12         //Win10 读写LocalMachine权限,没有访问权限
13         HKML = Registry.CurrentUser;
14         SOFTWARE = HKML.OpenSubKey(SUB_KEY, true);
15     }
16 
17     /// <summary>
18     /// 读取数据
19     /// </summary>
20     /// <param name="node">节点</param>
21     /// <param name="name">名称</param>
22     /// <returns></returns>
23     public static object GetData(string node, string name)
24     {
25         RegistryKey tmp = SOFTWARE.OpenSubKey(node, true);
26         return tmp?.GetValue(name);
27     }
28 
29     /// <summary>
30     /// 写入值
31     /// </summary>
32     /// <param name="node">节点</param>
33     /// <param name="name">名称</param>
34     /// <param name="value"></param>
35     public static void AddItem(string node, string name, object value)
36     {
37         RegistryKey tmp = SOFTWARE.CreateSubKey(node);
38         tmp?.SetValue(name, value);
39     }
40 }