日期:2012-10-26  浏览次数:20482 次

走近VB.Net(十二) 注册表快速入门
作者:hejianzhong VB.Net中文站(http://vbnetcn.126.com)
首先介绍一下注册表的相关基础知识:注册表的每一个文件夹被称为一个Key(项),这个文件夹的子文件夹被称为SubKey(子
项),而在一个子项中有不同的Value Name(值项—即数值顶),值项后面就是你要保存的数据Value Data(数据)了。而在子项中
通常都有一个Default Value 是默认的Value Name,相信打开过注册表的一定看得很清楚:
好了,我们现在看一下如何写入注册表。
最易的方法就是使用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.”的字样)
我在这里写了一个模块,只是为了示例,大家在使用的时候记住不需要像我这样另外使用模块(应该根据你的需要灵活运
用),这有些画蛇添足。这个我也不打算提供源码下载,因为实在太简单了。
首先如图添加控件:


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.L