三度网教程:是一个免费提供流行视频软件教程、在线学习分享的学习平台!

走近VB.Net(12) 注册表迅速基础

时间:2022-5-18作者:未知来源:三度网教程人气:


走近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.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 '_________________________________________________________________________________________________________________ '使用openremotebasekey方法打开一个注册表根项目,使用rgistryhive获取一个最上层的Key 根项目 Public Sub OpenBaseKey(ByVal basekey As Microsoft.Win32.RegistryHive) objkey = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(basekey, SystemInformation.ComputerName) End Sub '__________________________________________________________________________________________________ '使opensubkey打开一个指定的子项目, Public Sub NextSubKey(ByVal keyname As String, ByVal writeable As Boolean) objkey = objkey.OpenSubKey(keyname, writeable) '后面的布尔值(True,false)指定是否可读写 If objkey = keytemp Then End Sub '____________________________________________________________________________________________________________ '_______________________________________________________________________________________________________ '如果找不到就创造一个新的subkey子项目 Public Sub NewSubKey(ByVal keyname As String) objkey = objKey.CreateSubKey(keyname) '在这里我们创造了一个新的subkey(项) End Sub '____________________________________________________________________________________________________________________ '写入需要保存的数据-------ValueData Public Sub writeVal(ByVal valname As String, ByVal valdata As Object) objkey.SetValue(valname, valdata) '写入数据----ValueData End Sub ' ____________________________________________________________________ '读取保存的数据 Public Function getval(ByVal valname As String) As Object Return objKey.GetValue(valname) End Function '_________ '删除一个注册表项 Public Sub delKey(ByVal keyname As String) objkey.DeleteSubKey(keyname) End Sub '___________________________________________________________________________________________________________() '____________________________________________________________________________________________________________________ '删除一个数据------ValueData Public Sub delval(ByVal valname As String) objkey.DeleteValue(valname) '删除了一个值 objkey.Close() '记住在使用过后把他关闭,这通常是被提倡的好习惯。 End Sub End Module 大家看了是不是很简单,我曾在论坛看到人说,VB6用得很好我为什么要用VB.Net.而且Net只是Net技术,我们的这个……..不相关。这 真是天大的误解。就因为后面带了一个net,大家都不理解并疏远他,不论怎么样,VB.Net因为面向对象可以更有效率的写程序。大家为什么要拒绝呢? 我的系列文单出来以后,学VB.Net的人也增加了不少,我在网易论坛看到一个网友说为了装VB.Net装了一下午没装上,后来我告诉他到我的网站立马搞定了。 所以我并不反对张贴我的文章(我自已在讨论组宣传VB.Net又有打广告的嫌疑),可是你总要跟我打个招呼吧?我没有私心,一心为大家,可你们为什么要 把我的名字与网址隐藏起来呢?更有甚者,竟公然把文章作者换成自已的名字(在网易我的《VB.Net图解入门》《一步一步安装VB.Net》竟被改成"作者:大头鬼",---已被管理员删了!,)。看到这些,我的心里有点凉。前几天因为网速太慢,我通宵申请了一个高 速的网站,连夜上传所有资料,谁知上传又奇慢,两天,在网上呆了十几个小时。希望大家不要再这样的打击我了 ,这不是什么名利的问题,而是一个人起码的尊严被杜

关键词:  走近VB.Net(12)  注册表迅速基础





Copyright © 2012-2018 三度网教程(http://www.3du8.cn) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版