首页 > 编程 > .NET > 正文

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

2024-07-10 13:01:27
字体:
来源:转载
供稿:网友
走近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》竟被改成"作者:大头鬼",---已被管理员删了!,)。看到这些,我的心里有点凉。前几天因为网速太慢,我通宵申请了一个高 速的网站,连夜上传所有资料,谁知上传又奇慢,两天,在网上呆了十几个小时。希望大家不要再这样的打击我了 ,这不是什么名利的问题,而是一个人起码的尊严被杜渎了。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表