VB.net2008实例创建注册表编辑器
来源:岁月联盟
时间:2008-11-13
打开 Visual Studio 2008在文件 (File) 菜单上,单击新建项目 (New Project)。 在新建项目 (New Project) 对话框的模板 (Templates) 窗格中,单击 Windows 应用程序(Windows Application)。单击确定 (OK) 如图1。
选择Form1窗体,在Form1窗体中添加如下控件:
分别添加Label、Button、ComboBox 、TextBox、 ListBox类型的控件
控件详细属性设置如下:
接下来我们需要输入代码了,进入代码编辑器
首先需要进行声明:
Imports Microsoft.Win32.Registry
Public Class Form1
Inherits System.Windows.Forms.Form
Dim MyRegistry
进入HKey_SelectedIndexChanged事件
Private Sub HKey_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HKey.SelectedIndexChanged
Select Case HKey.Text
Case "ClassesRoot"
MyRegistry = Microsoft.Win32.Registry.ClassesRoot
Case "CurrentConfig"
MyRegistry = Microsoft.Win32.Registry.CurrentConfig
Case "CurrentUser"
MyRegistry = Microsoft.Win32.Registry.CurrentUser
Case "DynData"
MyRegistry = Microsoft.Win32.Registry.DynData
Case "LocalMachine"
MyRegistry = Microsoft.Win32.Registry.LocalMachine
Case "PerformanceData"
MyRegistry = Microsoft.Win32.Registry.PerformanceData
Case "Users"
MyRegistry = Microsoft.Win32.Registry.Users
End Select
End Sub
进入Button1_Click事件
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MyRegistry.CreateSubKey(SubKey.Text) '创建新键
Results.Items.Add("Create Key " & HKey.Text & "/" & SubKey.Text)
End Sub
进入Button2_Click事件
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MyRegistry.DeleteSubKey(SubKey.Text) '删除键
Results.Items.Add("Delete Key " & HKey.Text & "/" & SubKey.Text)
End Sub
进入Button3_Click事件
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
MyRegistry.DeleteSubKeyTree(SubKey.Text) '删除树
Results.Items.Add("Delete Tree " & HKey.Text & "/" & SubKey.Text)
End Sub
声明一个功能
Private Function GKEYString(ByVal KeyString As String)
Dim sLoc
sLoc = InStrRev(KeyString, "/")
If sLoc = 0 Then
GKEYString = KeyString
Else
GKEYString = Microsoft.VisualBasic.Left(KeyString, sLoc - 1)
End If
End Function 进入Button4_Click事件
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim TempReg '设置键
TempReg = MyRegistry.OpenSubKey(GKEYString(SubKey.Text), -1)
TempReg.SetValue(GKEYString(SubKey.Text), KeyValue.Text)
TempReg.Close()
Results.Items.Add("Set Value " & HKey.Text & "/" & SubKey.Text & " = " & KeyValue.Text)
End Sub
进入Button5_Click事件
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim TTReg
TTReg = MyRegistry.OpenSubKey(GKEYString(SubKey.Text), -1)
TTReg.DeleteValue(GKEYString(SubKey.Text))
TTReg.Close()
Results.Items.Add("Delete Value " & HKey.Text & "/" & SubKey.Text) '删除键值
End Sub
进入Button6_Click事件
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim TdTReg
TdTReg = MyRegistry.OpenSubKey(GKEYString(SubKey.Text), 0) '读取键
KeyValue.Text = TdTReg.GetValue(GKEYString(SubKey.Text), "")
TdTReg.Close()
Results.Items.Add("Read Value " & HKey.Text & "/" & SubKey.Text)
End Sub
代码输入完毕后,我们运行程序进行测试,输入子键与键值。运行界面如图3所示
好了程序运行成功,注册表应用是我们编程中常要用到的,希望本程序实例能够带给大家帮助,同时希望大家能够多去了解有关这方面的知识,有兴趣的朋友可以举一反三,利用这种思路可以编写出功能更强大的注册表编辑程序。