首页产品库评测行情新闻|手机数码笔记本台式机DIY硬件数字家庭数码相机办公外设|软件下载游戏开发|社区

更多

数码相机
MP4
LCD
机箱
音箱

天极网 > 软件频道>VB.NET注册表编程

VB.NET注册表编程

2003-08-22 15:49作者:符志强出处:微软社区责任编辑:方舟

  六.是如何删除注册表信息:

  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.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 )
software.DeleteSubKeyTree ( "ddd" )
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.OpenSubKey ( "ddd" , true )
ddd.DeleteValue( "www" )
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 reg02.vb



图4:用VB.NET删除注册表程序运行界面

  八:总结:

  至此我们已经比较全面的介绍了用VB.NET进行与注册表相关的各种编程,其中包括如何利用VB.NET来读取注册表,如何创建注册信息,如何修改注册信息,如何删除、重命名注册信息等。最后还要提醒一下,由于注册表是视窗的核心数据库,所以在程序中每一次对与注册表相关的操作都应该非常注意,做好备份工作,免得一次误操作导致系统的崩溃。
共3页。 9 1 2 3

关注此文的读者还看过:

返回软件频道首页

共3页。 上一页123

软件频道最新更新

热点推荐

天极服务|关于我们|About us|网站律师|RSS订阅|友情合作|加入我们|天极动态|网站地图|意见反馈|MSN/QQ上看天极
Copyright (C) 1999-2012 Yesky.com, All Rights Reserved 版权所有 天极网络