ddc
联网
平面设计 画册 VI欣赏 包装 CG-插画 搜索 个人网页 Alexa排名 CSS 建站资源 下载专区 JS特效 品牌服装 服装院校 专题欣赏 SEO 图标欣赏 专题
网站建设 域名注册 网站建设 虚拟主机 广州网站设计 域名注册 广州网站建设 上海网站建设 虚拟主机 广州网页设计 虚拟主机 域名注册 acg王国 ACG玩家 品牌设计 上海网站建设
求创科技
网站建设
中国互联
素材出售
中国设计秀
中资源
当前位置:网络学院首页 >> 编程开发 >> .net >> VB.NET中快速访问注册表技巧

VB.NET中快速访问注册表技巧 (1)

来源:中国设计秀    作者:rux    点击:34     加入收藏    发表评论
0
顶一下
关键字:VB.NET

  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")

[1] [2]
2008-09-18 10:43:00    出处:中国设计秀cnwebshow.com
Google
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明 | 网站公告 | 友情链接 | 留言 | 旧版入口