WinCE下VB.NET读写注册表的参考代码
与桌面Windows系统类似,WinCE将有关操作系统、应用程序和驱动的设置信息都保存在系统注册表中。WinCE的注册表与桌面系统中的组织方式也差不多,包括了如下四个根键:
HKEY_CLASS_ROOT,包含了文件扩展名信息和COM子系统的配置信息
HKEY_LOCAL_MACHINE,包含了系统、驱动和应用程序的配置信息
HKEY_CURRENT_USER,包含了当前用户的配置信息
HKEY_USER,包含了所有用户的信息
WinCE提供了一系列的用于操作注册表的API,之前做的驱动调试助手小工具就是利用这些API实现的。那VB.NET如何操作注册表呢?实际上更简单,.NET Compact Framework提供了Registry和RegistryKey,可以利用这两个类,很方便的实现注册表的读写。
下面以修改WinCE设备的名称及描述为例,简单介绍一下WinCE下VB.NET读写注册表的方法。参考代码如下。
1 Imports Microsoft.Win32
2
3 Public Class DevName
4
5 Private Sub ButtonSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSet.Click
6 Dim hklm As RegistryKey = Registry.LocalMachine
7 Dim subkey As RegistryKey = hklm.OpenSubKey("Ident", True)
8 subkey.SetValue("Name", TextBoxDevName.Text)
9 subkey.SetValue("Desc", TextBoxDevDesc.Text)
10 End Sub
11
12 Private Sub ButtonGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonGet.Click
13 Dim hklm As RegistryKey = Registry.LocalMachine
14 Dim subkey As RegistryKey = hklm.OpenSubKey("Ident", False)
15 TextBoxDevName.Text = subkey.GetValue("Name")
16 TextBoxDevDesc.Text = subkey.GetValue("Desc")
17 End Sub
18
19 Private Sub ButtonClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonClear.Click
20 TextBoxDevName.Text = ""
21 TextBoxDevDesc.Text = ""
22 End Sub
23 End Class
2
3 Public Class DevName
4
5 Private Sub ButtonSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSet.Click
6 Dim hklm As RegistryKey = Registry.LocalMachine
7 Dim subkey As RegistryKey = hklm.OpenSubKey("Ident", True)
8 subkey.SetValue("Name", TextBoxDevName.Text)
9 subkey.SetValue("Desc", TextBoxDevDesc.Text)
10 End Sub
11
12 Private Sub ButtonGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonGet.Click
13 Dim hklm As RegistryKey = Registry.LocalMachine
14 Dim subkey As RegistryKey = hklm.OpenSubKey("Ident", False)
15 TextBoxDevName.Text = subkey.GetValue("Name")
16 TextBoxDevDesc.Text = subkey.GetValue("Desc")
17 End Sub
18
19 Private Sub ButtonClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonClear.Click
20 TextBoxDevName.Text = ""
21 TextBoxDevDesc.Text = ""
22 End Sub
23 End Class
如果WinCE系统支持HIVE注册表,还可以通过RegistryKey.Flush()或RegistryKey.Close(),将修改的内容刷新的磁盘。