欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

在VB.NET中如何快速访问注册表的技巧 代码

程序员文章站 2022-06-18 10:29:53
vb.net中访问注册表变得非常的简单。我们可以用microsoft.Win32 ... 12-05-23...

vb.net中访问注册表变得非常的简单。我们可以用microsoft.win32 名称空间的下的registry类和registrykey类。另外my.computer.registry 也可以返回一个microsoft.win32.registry类的实例。
  下面就举几个小例子来说明vb.net访问注册表的方法。
  1、返回或创建一个注册表键

  dim key1 as microsoft.win32.registrykey
key1 = my.computer.registry.currentuser '返回当前用户键
dim key2 as microsoft.win32.registrykey
key2 = key1.opensubkey("northsnow") '返回当前用户键下的northsnow键
if key2 is nothing then
key2 = key1.createsubkey("northsnow") '如果键不存在就创建它
end if

  2、删除注册表键

  dim key1 as microsoft.win32.registrykey
key1 = my.computer.registry.currentuser '返回当前用户键
dim key2 as microsoft.win32.registrykey
key2 = key1.opensubkey("northsnow") '返回当前用户键下的northsnow键
if not key2 is nothing then
key1.deletesubkey("northsnow") '如果键不存在就创建它
end if

  3、创建或读取注册表项  

  dim key1 as microsoft.win32.registrykey
key1 = my.computer.registry.currentuser '返回当前用户键
dim key2 as microsoft.win32.registrykey
key2 = key1.opensubkey("northsnow", true) '返回当前用户键下的northsnow键,

  如果想创建项,必须指定第二个参数为true
if key2 is nothing then
key2 = key1.createsubkey("northsnow") '如果键不存在就创建它
end if
'创建项,如果不存在就创建,如果存在则覆盖
key2.setvalue("name", "塞北的雪")
key2.setvalue("sex", true)
key2.setvalue("age", 30)
'返回项值
dim sb as new system.text.stringbuilder
sb.appendline(key2.getvalue("name"))
sb.appendline(key2.getvalue("sex"))
sb.appendline(key2.getvalue("age"))
msgbox(sb.tostring)
'查验某个项是否存在
if (key2.getvalue("name")) is nothing then
msgbox("no")
else
msgbox("yes")
end if
if (key2.getvalue("name2")) is nothing then
msgbox("no")
else
msgbox("yes")
end if

  '输出
' 塞北的雪
'true
'30
'yes
'no


4、遍历注册表
  这个也非常简单,  在窗体上放一个按钮和两个文本框,添加如下的代码:

  dim sb as new system.text.stringbuilder '返回遍历结果
dim sb2 as new system.text.stringbuilder '返回读取出错的注册表键
private sub button3_click()sub button3_click(byval sender as system.object,

  byval e as system.eventargs) handles button3.click
dim key1 as microsoft.win32.registrykey
key1 = my.computer.registry.currentuser '返回当前用户键
if not key1 is nothing then
sb.appendline(key1.name)
readvalue(key1)
readreg(key1)
end if
me.textbox1.text = sb.tostring
me.textbox2.text = sb2.tostring
end sub
'遍历注册表键树
private sub readreg()sub readreg(byval r as microsoft.win32.registrykey)
if r.subkeycount > 0 then
dim keyname() as string
dim keytemp as microsoft.win32.registrykey
keyname = r.getsubkeynames
dim i as integer
for i = 0 to keyname.getlength(0) - 1
try
sb.appendline(keyname(i))
keytemp = r.opensubkey(keyname(i), true)
readvalue(keytemp)
readreg(keytemp)
catch ex as exception
sb2.appendline(keyname(i))
end try
next
end if
end sub
'遍历某键下的项
private sub readvalue()sub readvalue(byval r as microsoft.win32.registrykey)
if r.valuecount > 0 then
dim valuename() as string
dim i as integer
valuename = r.getvaluenames
for i = 0 to valuename.getlength(0) - 1
sb.appendline("####")
sb.append(r.name)
sb.append("----")
sb.append(r.getvalue(valuename(i)).tostring)
next
end if
end sub