名字空间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![](/Images/OutliningIndicators/None.gif)
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![](/Images/OutliningIndicators/None.gif)
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![](/Images/OutliningIndicators/None.gif)
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![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/None.gif)
14
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
20
![](/Images/OutliningIndicators/None.gif)
21
![](/Images/OutliningIndicators/None.gif)
22
![](/Images/OutliningIndicators/None.gif)
23
![](/Images/OutliningIndicators/None.gif)
24
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
35
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
36
![](/Images/OutliningIndicators/None.gif)
37
![](/Images/OutliningIndicators/None.gif)
38
![](/Images/OutliningIndicators/None.gif)
39
![](/Images/OutliningIndicators/None.gif)
40
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
54
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
57
![](/Images/OutliningIndicators/None.gif)
以上是在注册表中HKEY_LOCAL_MACHINE\SOFTWARE目录下XXX目录中判断名称为name注册表项是否存在,这一方法在删除注册表时已经存在,在新建一注册表项时也应有相应判断;