名字空间Microsoft.Win32
以下从‘读’‘写’‘删除’‘判断’四个事例实现对注册表的简单操作
1.读取指定名称的注册表的值
以上是在注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下XXX目录中判断名称为name注册表项是否存在,这一方法在删除注册表时已经存在,在新建一注册表项时也应有相应判断;
以下从‘读’‘写’‘删除’‘判断’四个事例实现对注册表的简单操作
1.读取指定名称的注册表的值
1
private
string
GetRegistData(
string
name)
2 {
3 string registData;
4 RegistryKey hkml = Registry.LocalMachine;
5 RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
6 RegistryKey aimdir = software.OpenSubKey("XXX",true);
7 registData = aimdir.GetValue(name).ToString();
8 return registData;
9}
10 以上是读取的注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下的XXX目录中名称为name的注册表值;
11
12 2 .向注册表中写数据
13 private void WTRegedit( string name, string tovalue)
14 {
15 RegistryKey hklm = Registry.LocalMachine;
16 RegistryKey software = hklm.OpenSubKey("SOFTWARE",true);
17 RegistryKey aimdir = software.CreateSubKey("XXX");
18 aimdir.SetValue(name,tovalue);
19}
20 以上是在注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下新建XXX目录并在此目录下创建名称为name值为tovalue的注册表项;
21
22 3 .删除注册表中指定的注册表项
23 private void DeleteRegist( string name)
24 {
25 string[] aimnames;
26 RegistryKey hkml = Registry.LocalMachine;
27 RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
28 RegistryKey aimdir = software.OpenSubKey("XXX",true);
29 aimnames = aimdir.GetSubKeyNames();
30 foreach(string aimKey in aimnames)
31 {
32 if(aimKey == name)
33 aimdir.DeleteSubKeyTree(name);
34 }
35}
36 以上是在注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下XXX目录中删除名称为name注册表项;
37
38 4 .判断指定注册表项是否存在
39 private bool IsRegeditExit( string name)
40 {
41 bool _exit = false;
42 string[] subkeyNames;
43 RegistryKey hkml = Registry.LocalMachine;
44 RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
45 RegistryKey aimdir = software.OpenSubKey("XXX",true);
46 subkeyNames = aimdir.GetSubKeyNames();
47 foreach(string keyName in subkeyNames)
48 {
49 if(keyName == name)
50 {
51 _exit = true;
52 return _exit;
53 }
54 }
55 return _exit;
56}
57
2 {
3 string registData;
4 RegistryKey hkml = Registry.LocalMachine;
5 RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
6 RegistryKey aimdir = software.OpenSubKey("XXX",true);
7 registData = aimdir.GetValue(name).ToString();
8 return registData;
9}
10 以上是读取的注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下的XXX目录中名称为name的注册表值;
11
12 2 .向注册表中写数据
13 private void WTRegedit( string name, string tovalue)
14 {
15 RegistryKey hklm = Registry.LocalMachine;
16 RegistryKey software = hklm.OpenSubKey("SOFTWARE",true);
17 RegistryKey aimdir = software.CreateSubKey("XXX");
18 aimdir.SetValue(name,tovalue);
19}
20 以上是在注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下新建XXX目录并在此目录下创建名称为name值为tovalue的注册表项;
21
22 3 .删除注册表中指定的注册表项
23 private void DeleteRegist( string name)
24 {
25 string[] aimnames;
26 RegistryKey hkml = Registry.LocalMachine;
27 RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
28 RegistryKey aimdir = software.OpenSubKey("XXX",true);
29 aimnames = aimdir.GetSubKeyNames();
30 foreach(string aimKey in aimnames)
31 {
32 if(aimKey == name)
33 aimdir.DeleteSubKeyTree(name);
34 }
35}
36 以上是在注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下XXX目录中删除名称为name注册表项;
37
38 4 .判断指定注册表项是否存在
39 private bool IsRegeditExit( string name)
40 {
41 bool _exit = false;
42 string[] subkeyNames;
43 RegistryKey hkml = Registry.LocalMachine;
44 RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
45 RegistryKey aimdir = software.OpenSubKey("XXX",true);
46 subkeyNames = aimdir.GetSubKeyNames();
47 foreach(string keyName in subkeyNames)
48 {
49 if(keyName == name)
50 {
51 _exit = true;
52 return _exit;
53 }
54 }
55 return _exit;
56}
57
以上是在注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下XXX目录中判断名称为name注册表项是否存在,这一方法在删除注册表时已经存在,在新建一注册表项时也应有相应判断;