科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件VB.NET注册表编程

VB.NET注册表编程

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

在注册表中,子树是主要节点,包括健、子健和值。健就是打开\\\"注册表编辑器\\\"后

作者:马金虎 来源:yesky 2007年10月15日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
六.是如何删除注册表信息:

  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


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

  八:总结:

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

查看本文来源

>
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章