VB.NET注册表编程 作者:马金虎 点击数:402 文章来源:yesky 更新时间:2007-8-30 一.程序设计和运行的环境设置: (1).视窗2000服务器版 (2). .Net FrameWork SDK Beta 2 二.初步认识注册表: 首先我们来了解一下注册表的组成。在注册表中,子树是主要节点,包括健、子健和值。健就是打开"注册表编辑器"后,出现在"注册表编辑器"左窗格中的文件夹,健可以包含子健和值健。子健就是健中的健,在注册表中,子健属于树和健。值健就是运行"注册表编辑器"后,出现在"注册表编辑器"右窗格中的数据字符串,他定义了当前所选健的值,值健由三个部分组成:名称、数据类型和值本身。下图就是注册表的各个组成部分: 图01:注册表组成图表 三.设定测试环境: 由于注册表在视窗系统中的特殊地位,我们自己定义几个作为程序试验使用。在注册表健"SYSTEM"下面创建一个子健"A000",在此子健下面再创建二个子健"B000"和"B001"。在子健"B000"中创建一个健名称为"0001",数据类型为字符串,值为"1111";在子健"B001"中也创建一个健,名称为"0002",数据类型为字符串,值为"2222"。下图就是我们为本文中介绍的程序所设定的注册表环境: 图02:本文中介绍的程序使用的注册表 四.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.LocalMachineDim 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 SystemImports System.DrawingImports System.ComponentModelImports System.Windows.FormsImports Microsoft.Win32'导入程序中使用的名称空间Public Class Form1Inherits FormPublic Sub New ( )MyBase.New ( )'初始化窗体中的各个组件InitializeComponent ( )End Sub'清除程序中使用过的各种资源Protected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean )If disposing ThenIf Not ( components Is Nothing ) Thencomponents.Dispose ( )End IfEnd IfMyBase.Dispose ( disposing )End SubFriend WithEvents Button1 As ButtonFriend WithEvents listBox1 As ListBoxFriend WithEvents Button2 As ButtonFriend WithEvents Button3 As ButtonPrivate 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 = 12Me.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 SubPrivate Sub Button1_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) _Handles Button1.ClicklistBox1.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 integerFor i = 0 to KeyCount - 1listBox1.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 integerFor j = 0 to ValueCount - 1listBox1.Items.Add ( " " + Str2 ( j ) + ": " + sitekey.GetValue ( Str2 ( j ) ) ) '在列表中加入所有子健、健和健值Next jNext i End Sub'创建子健,并创建一个健并赋值Private Sub Button2_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) _Handles Button2.ClicklistBox1.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.ClicklistBox1.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 SubEnd ClassModule Module1Sub Main ( )Application.Run ( New Form1 ( ) )End SubEnd Module 经过下列命令编译后,这些可以得到下面界面: vbc /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll reg01.vb 图03:用VB.NET创建和修改注册表程序运行界面 六.是如何删除注册表信息: VB.NET删除注册表中的信息也是通过RegistryKey类中封装的方法来实现,具体的说,就是DeleteSubKey ( )方法、DeleteSubKeyTree ( )方法和DeleteValue ( )方法。其中 1). DeleteSubKey ( )方法: 在使用此方法删除一个指定的子键的时候,要确保此子健下面不能存在子健,否则会产生一个错误信息。在程序中调用此方法有二种原型: I > . DeleteSubKey ( string , subkey ):这种调用方式就是直接删除指定的子健。 II > . DeleteSubKey ( string subkey , Boolean info ):其中的"string"是要删除的子健的名称,"Boolean"参数的意思是:如果值为"True",则在程序调用的时候,删除的子健不存在,则产生一个错误信息;如果值为"False",则在程序调用的时候,删除的子健不存在,也不产生错误信息,程序依然正确运行。所以在具体的程序设计过程中,我还是推荐使用第二种调用方法。 (2). DeleteSubKeyTree ( )方法: 此方法是彻底删除指定的子健目录,即:删除该子健以及该子健以下的全部子健。由于此方法的破坏性是非常强的,所以在使用的时候要非常注意。在程序中调用此方法的原型为: DeleteSubKeyTree ( string subkey ):其中"subkey"就是要彻底删除的子健名称。 下面这一段代码功能就是删除已经打开子健"A000"下面的子健"ddd": Dim hklm As RegistryKey = Registry.LocalMachine '打开"SYSTEM"子健Dim software11 As RegistryKey = hklm.OpenSubKey ( "SYSTEM" ,true ) '打开"A000"子健Dim software As RegistryKey = software11.OpenSubKey ( "A000" , true ) software.DeleteSubKeyTree ( "ddd" ) (3). DeleteValue ( )方法: 此方法是删除指定的健值。在程序中调用此方法的原型为: DeleteValue ( string value ):其中"value"就是要删除的健值的名称。 下面这一段代码的作用就是删除子健"ddd"中的健"www": 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.OpenSubKey ( "ddd" , true ) ddd.DeleteValue( "www" ) 七.VB.NET删除注册表信息程序代码(reg02.vb): 通过上面的那个例子,结合以上的知识,我们可以得到用来删除有reg01.vb所创建的注册表信息的程序代码(reg02.vb),代码如下: Private Sub Button1_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) _Handles Button1.ClicklistBox1.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 integerFor i = 0 to KeyCount - 1listBox1.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 integerFor j = 0 to ValueCount - 1listBox1.Items.Add ( " " + Str2 ( j ) + ": " + sitekey.GetValue ( Str2 ( j ) ) ) '在列表中加入所有子健、健和健值Next jNext i End Sub'删除子健Private Sub Button2_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) _Handles Button2.ClicklistBox1.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 ) software.DeleteSubKeyTree ( "ddd" )End Sub'删除指定的健Private Sub Button3_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) _Handles Button3.ClicklistBox1.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.OpenSubKey ( "ddd" , true ) ddd.DeleteValue( "www" ) End SubEnd ClassModule Module1Sub Main ( )Application.Run ( New Form1 ( ) )End SubEnd Module 经过如下命令编译后,可以得到如下运行界面: vbc /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll reg02.vb 图04:用VB.NET删除注册表程序运行界面 八:总结: 至此我们已经比较全面的介绍了用VB.NET进行与注册表相关的各种编程,其中包括如何利用VB.NET来读取注册表,如何创建注册信息,如何修改注册信息,如何删除、重命名注册信息等。最后还要提醒一下,由于注册表是视窗的核心数据库,所以在程序中每一次对与注册表相关的操作都应该非常注意,做好备份工作,免得一次误操作导致系统的崩溃。

评论