四.VB.NET是如何创建注册表信息:
在.Net Framework SDK中定义了一个名称空间--Microsoft.Win32,这个名称空间中封装了用于操作注册表的许多类,在具体的程序设计中,主要用到的是:Registry类、RegistryKey类。其中Registry类主要是提供为存取值和子健所必须的基本的子目录树。在Registry类中定义了注册表中7个主要的子目录树。其对应如下:
Registry.ClassesRoot 对应于HKEY_CLASSES_ROOT子目录树
Registry.CurrentUser 对应于HKEY_CURRENT_USER子目录树
Registry.LocalMachine 对应于 HKEY_LOCAL_MACHINE子目录树
Registry.User 对应于 HKEY_USER子目录树
Registry.CurrentConfig 对应于HEKY_CURRENT_CONFIG子目录树
Registry.DynDa 对应于HKEY_DYN_DATA子目录树
Registry.PerformanceData 对应于HKEY_PERFORMANCE_DATA子目录树
VB.NET主要是利用RegistryKey类封装的方法、属性等来进行与注册表相关的各种操作。下面就来介绍一下,如何用VB.NET创建注册表信息。在创建注册信息,首先必须要知道如何确定要创建注册信息的地方。譬如本文我们是要在子健"A000"下面再创建一个子健,那么首先通过下列语句就可以使得注册表的记录指针指向"A000",具体如下:
Dim hklm As RegistryKey = Registry.LocalMachine Dim software11 As RegistryKey = hklm.OpenSubKey ( "SYSTEM" ) '打开"SYSTEM"子健 Dim software As RegistryKey = software11.OpenSubKey ( "A000" ) '打开"A000"子健 | 方法"OpenSubKey"被定义在RegistryKey类中的,此方法可以看成是确定注册表记录的指针。此方法的调用主要有二种,一种当打开指定的子健的目的是为了读取,那么调用方法可以采用上面这种方式;如果打开指定子健的目的是为了进行写操作,那么在使用的时候必须采用下面语法:
| OpenSubKey ( 子健名称 , true ) | 在RegistryKey类中定义了CreateSubKey ( )来创建子健,通过SetValue ( )方法来创建健,并为此健赋值。下面语句就是在已经打开的子健"A000"下面创建一个子健并赋值:
listBox1.Items.Clear ( ) Dim hklm As RegistryKey = Registry.LocalMachine '打开"SYSTEM"子健 Dim software11 As RegistryKey = hklm.OpenSubKey ( "SYSTEM" ,true ) '打开"A000"子健 Dim software As RegistryKey = software11.OpenSubKey ( "A000" , true ) Dim ddd As RegistryKey = software.CreateSubKey ( "ddd" ) ddd.SetValue ( "www" , "1234" ) | 修改健值可以通过SetValue ( )来实现,具体的语法如下:
SetValue (存在的健 ,新的健值)
五.VB.NET是创建并修改注册表信息的程序代码(reg01.vb):
至此我们不难得到用VB.NET创建并修改注册表的程序代码,在下列代码中,有一点要说明,就是在程序中使用了RegistryKey类中定义另外二个属性:SubKeyCount和ValueCount,其中第一个属性是当前子健下面有多少个子健,第二个是当前子健中定义了多少个健。
Imports System Imports System.Drawing Imports System.ComponentModel Imports System.Windows.Forms Imports Microsoft.Win32 '导入程序中使用的名称空间 Public Class Form1 Inherits Form Public Sub New ( ) MyBase.New ( ) '初始化窗体中的各个组件 InitializeComponent ( ) End Sub '清除程序中使用过的各种资源 Protected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean ) If disposing Then If Not ( components Is Nothing ) Then components.Dispose ( ) End If End If MyBase.Dispose ( disposing ) End Sub Friend WithEvents Button1 As Button Friend WithEvents listBox1 As ListBox Friend WithEvents Button2 As Button Friend WithEvents Button3 As Button
Private components As System.ComponentModel.Container '初始化程序中使用到的组件 Private Sub InitializeComponent ( ) Me.listBox1 = New ListBox ( ) Me.Button1 = New Button ( ) Me.Button2 = New Button ( ) Me.Button3 = New Button ( ) Me.SuspendLayout ( ) Me.listBox1.ItemHeight = 12 Me.listBox1.Location = New Point ( 8 , 24 ) Me.listBox1.Name = "listBox1" Me.listBox1.Size = New Size ( 480 , 292 ) Me.listBox1.TabIndex = 1
...下略
Me.Controls.Add ( Me.listBox1 ) Me.Controls.Add ( Me.Button1 ) Me.Controls.Add ( Me.Button2 ) Me.Controls.Add ( Me.Button3 ) Me.Name = "Form1" Me.Text = "用VB.NET进行注册表编程!" Me.ResumeLayout ( False )
End Sub
Private Sub Button1_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) _ Handles Button1.Click listBox1.Items.Clear ( ) Dim hklm As RegistryKey = Registry.LocalMachine Dim software11 As RegistryKey = hklm.OpenSubKey ( "SYSTEM" ) '打开"SYSTEM"子健 Dim software As RegistryKey = software11.OpenSubKey ( "A000" ) '打开"A000"子健 Dim KeyCount As integer = software.SubKeyCount '获得当前健下面有多少子健 Dim Str ( ) As String = software.GetSubKeyNames ( ) '获得当前健下面所有子健组成的字符串数组 Dim i As integer For i = 0 to KeyCount - 1 listBox1.Items.Add ( Str ( i ) ) Dim sitekey As RegistryKey = software.OpenSubKey ( Str ( i ) ) '按顺序打开子健 Dim Str2 ( ) As String = sitekey.GetValueNames ( ) '获得当前子健下面所有健组成的字符串数组 Dim ValueCount As integer = sitekey.ValueCount '获得当前子健存在多少健值 Dim j As integer For j = 0 to ValueCount - 1 listBox1.Items.Add ( " " + Str2 ( j ) + ": " + sitekey.GetValue ( Str2 ( j ) ) ) '在列表中加入所有子健、健和健值 Next j Next i End Sub '创建子健,并创建一个健并赋值 Private Sub Button2_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) _ Handles Button2.Click listBox1.Items.Clear ( ) Dim hklm As RegistryKey = Registry.LocalMachine '打开"SYSTEM"子健 Dim software11 As RegistryKey = hklm.OpenSubKey ( "SYSTEM" ,true ) '打开"A000"子健 Dim software As RegistryKey = software11.OpenSubKey ( "A000" , true ) Dim ddd As RegistryKey = software.CreateSubKey ( "ddd" ) ddd.SetValue ( "www" , "1234" ) End Sub '重命名一个健的值 Private Sub Button3_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) _ Handles Button3.Click listBox1.Items.Clear ( ) Dim hklm As RegistryKey = Registry.LocalMachine '打开"SYSTEM"子健 Dim software11 As RegistryKey = hklm.OpenSubKey ( "SYSTEM" ,true ) '打开"A000"子健 Dim software As RegistryKey = software11.OpenSubKey ( "A000" , true ) Dim ddd As RegistryKey = software.CreateSubKey ( "ddd" ) ddd.SetValue ( "www" , "aaaa" ) End Sub
End Class Module Module1 Sub Main ( ) Application.Run ( New Form1 ( ) ) End Sub End Module | 经过下列命令编译后,这些可以得到下面界面:
vbc /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll reg01.vb

图3:用VB.NET创建和修改注册表程序运行界面
|
|