走近VB.Net(十二) 注册表快速入门

走近VB.Net(十二) 注册表快速入门

                      作者:hejianzhong VB.Net中文站(http://vbnetcn.126.com)


   首先介绍一下注册表的相关基础知识:注册表的每一个文件夹被称为一个Key(项),这个文件夹的子文件夹被称为SubKey(子


项),而在一个子项中有不同的Value Name(值项—即数值顶),值项后面就是你要保存的数据Value Data(数据)了。而在子项中


通常都有一个Default Value 是默认的Value Name,相信打开过注册表的一定看得很清楚:  
![](http://my.6to23.com/vbnetcn/images/REG2.GIF)


                     好了,我们现在看一下如何写入注册表。


     最易的方法就是使用VB内置的函数了,这与VB6中使用的方法一模一样:


————————————————————————————————————————


设置Sub SaveSetting(AppName As String,Section As String,Key As String,Setting As String)


获取 Function GetSetting(AppName As String,Section As String,Key As String,[Default As String])


删除DeleteSetting(AppName As String,[Section As String=nothing],[Key As String=nothing] )


————————————————————————————————————————————————


     不过他只能写在一个固定的位置,写入HKEY_CURRENT_USER//Software//VB and VBA Program Setting.当我们需要使


用注册表加密时,总不能写在这样一个大家都知道的地方吧?还有当我们要使用注册表实现一些功能时(如写入run主键让程序


启动时自运行)这个更是无能为力。相信大家都看过VB6的例程,很是复杂。不过我看的一个系列文章写得很好(在我的网站


---VB6知识库中有收录)竟有七八页。在VB.Net中就很简单了,跟上面真有些差不多。--------(不过我可是研究了很久的,很辛


苦---最近有人把我的文章改成自已的名字,我很伤心,也请这些人自重,再次声明在任何地方张贴必须经过我的同意,并明确


标明:“作者:hejianzhong  VB.Net中文站 http:://vbnetcn.126.com.”的字样)


我在这里写了一个模块,只是为了示例,大家在使用的时候记住不需要像我这样另外使用模块(应该根据你的需要灵活运


用),这有些画蛇添足。这个我也不打算提供源码下载,因为实在太简单了。


      首先如图添加控件:


![](http://my.6to23.com/vbnetcn/images/REG1.GIF)


 


      textbox控件的text设为“”,其它的请作相应的修改,实际上这个示例你不编也可以,只要看懂下面的内容就行了:


写代码如下:


 


Imports System.ComponentModel 

Imports System.Drawing 

Imports System.WinForms 

  

  

Public Class Form1 

    Inherits System.WinForms.Form 

  

    Public Sub New() 

        MyBase.New 

  

        Form1 = Me 

  

        'This call is required by the Win Form Designer. 

        InitializeComponent 

  

        'TODO: Add any initialization after the InitializeComponent() call 

    End Sub 

  

    'Form overrides dispose to clean up the component list. 

    Overrides Public Sub Dispose() 

        MyBase.Dispose 

        components.Dispose 

    End Sub 

下面这些 

#Region " Windows Form Designer generated code " 

  

    'Required by the Windows Form Designer 

    Private components As System.ComponentModel.Container 

    

    Private WithEvents Label2 As System.WinForms.Label 

    

    

    Private WithEvents Label1 As System.WinForms.Label 

    Private WithEvents TextBox2 As System.WinForms.TextBox 

    

    

    Private WithEvents Button4 As System.WinForms.Button 

    Private WithEvents Button3 As System.WinForms.Button 

    

    Private WithEvents TextBox1 As System.WinForms.TextBox 

    Private WithEvents Button1 As System.WinForms.Button 

    

    

    Private WithEvents RadioButton2 As System.WinForms.RadioButton 

    Private WithEvents RadioButton1 As System.WinForms.RadioButton 

    

    Dim WithEvents Form1 As System.WinForms.Form 

  

    'NOTE: The following procedure is required by the Windows Form Designer 

    'It can be modified using the Windows Form Designer.  

    'Do not modify it using the code editor. 

    Private Sub InitializeComponent() 

        Me.components = New System.ComponentModel.Container() 

        Me.TextBox1 = New System.WinForms.TextBox() 

        Me.TextBox2 = New System.WinForms.TextBox() 

        Me.Label2 = New System.WinForms.Label() 

        Me.Label1 = New System.WinForms.Label() 

        Me.Button4 = New System.WinForms.Button() 

        Me.RadioButton1 = New System.WinForms.RadioButton() 

        Me.Button3 = New System.WinForms.Button() 

        Me.Button1 = New System.WinForms.Button() 

        Me.RadioButton2 = New System.WinForms.RadioButton() 

         

        '@design Me.TrayHeight = 0 

        '@design Me.TrayLargeIcon = False 

        '@design Me.TrayAutoArrange = True 

        TextBox1.Location = New System.Drawing.Point(184, 80) 

        TextBox1.Multiline = True 

        TextBox1.TabIndex = 3 

        TextBox1.Size = New System.Drawing.Size(144, 24) 

        

        TextBox2.Location = New System.Drawing.Point(184, 128) 

        TextBox2.Multiline = True 

        TextBox2.TabIndex = 7 

        TextBox2.Size = New System.Drawing.Size(144, 24) 

        

        Label2.Location = New System.Drawing.Point(64, 192) 

        Label2.Text = "Label2" 

        Label2.Size = New System.Drawing.Size(264, 32) 

        Label2.TabIndex = 9 

        

        Label1.Location = New System.Drawing.Point(16, 184) 

        Label1.Text = "当前位置" 

        Label1.Size = New System.Drawing.Size(40, 32) 

        Label1.TabIndex = 8 

        

        Button4.Location = New System.Drawing.Point(8, 152) 

        Button4.Size = New System.Drawing.Size(168, 24) 

        Button4.TabIndex = 6 

        Button4.Text = "读取数据" 

        

        RadioButton1.Location = New System.Drawing.Point(8, 8) 

        RadioButton1.Text = "写入HKEY_CURRENT_USER " 

        RadioButton1.Size = New System.Drawing.Size(168, 24) 

        RadioButton1.TabIndex = 0 

        

        Button3.Location = New System.Drawing.Point(8, 112) 

        Button3.Size = New System.Drawing.Size(168, 24) 

        Button3.TabIndex = 5 

        Button3.Text = "写入数据" 

        

        Button1.Location = New System.Drawing.Point(8, 80) 

        Button1.Size = New System.Drawing.Size(168, 24) 

        Button1.TabIndex = 2 

        Button1.Text = "打开注册表子项(subkey)" 

        

        RadioButton2.Location = New System.Drawing.Point(8, 40) 

        RadioButton2.Text = "写入HKEY_LOCAL_MACHINE " 

        RadioButton2.Size = New System.Drawing.Size(168, 40) 

        RadioButton2.TabIndex = 1 

        Me.Text = "Form1" 

        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14) 

        Me.ClientSize = New System.Drawing.Size(336, 277) 

        

        Me.Controls.Add(Label2) 

        Me.Controls.Add(Label1) 

        Me.Controls.Add(TextBox2) 

        Me.Controls.Add(Button4) 

        Me.Controls.Add(Button3) 

        Me.Controls.Add(TextBox1) 

        Me.Controls.Add(Button1) 

        Me.Controls.Add(RadioButton2) 

        Me.Controls.Add(RadioButton1) 

    End Sub 

    

#End Region 

    

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) 

        textbox2.Text = getval("数值项").ToString 

    End Sub 

    

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) 

        writeval("数值项", textbox2.Text) : textbox2.Text = "已写入到注册表" 

    End Sub 

    

    

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) 

        If objkey = Nothing Then 

            msgbox("请先在上面选定注册表根项") 

            Me.RadioButton1.Select() 

            OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser) 

            label2.Text = objkey.ToString 

            Exit Sub 

        End If 

        newsubkey(textbox1.Text) 

        label2.Text = objkey.ToString 

    End Sub 

    

    Protected Sub RadioButton2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) 

        openbasekey(Microsoft.Win32.RegistryHive.LocalMachine) 

        label2.Text = objkey.ToString 

    End Sub 

    

    Protected Sub RadioButton1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) 

        OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser) 

        label2.Text = objkey.ToString 

    End Sub 

End Class 

  

  

  

  

'################################################################################################################## 

Public Module RWregKey 

    '定义一个objKey 的注册项对象 

    Public objKey As Microsoft.Win32.RegistryKey 

    

    '_________________________________________________________________________________
Published At
Categories with Web编程
Tagged with
comments powered by Disqus