讲解VB.NET访问注册表方法

开发 后端
这里介绍VB.NET访问注册表变得非常的简单。我们可以用microsoft.Win32 名称空间的下的registry类和registryKey类。另外My.Computer.Registry 也可以返回一个Microsoft.Win32.Registry类的实例。

本文向大家介绍VB.NET访问注册表,可能好多人还不了解VB.NET访问注册表,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西。

VB.NET访问注册表变得非常的简单。我们可以用microsoft.Win32 名称空间的下的registry类和registryKey类。另外My.Computer.Registry 也可以返回一个Microsoft.Win32.Registry类的实例。

下面就举几个小例子来说明VB.NET访问注册表的方法。

1、返回或创建一个注册表键

  1. Dim Key1 As Microsoft.Win32.RegistryKey   
  2. Key1 = My.Computer.Registry.CurrentUser   
  3. '返回当前用户键   
  4. Dim Key2 As Microsoft.Win32.RegistryKey   
  5. Key2 = Key1.OpenSubKey("northsnow")   
  6. '返回当前用户键下的northsnow键   
  7. If Key2 Is Nothing Then   
  8. Key2 = Key1.CreateSubKey("northsnow")   
  9. '如果键不存在就创建它   
  10. End If  

2、删除注册表键

  1. Dim Key1 As Microsoft.Win32.RegistryKey   
  2. Key1 = My.Computer.Registry.CurrentUser   
  3. '返回当前用户键   
  4. Dim Key2 As Microsoft.Win32.RegistryKey   
  5. Key2 = Key1.OpenSubKey("northsnow")   
  6. '返回当前用户键下的northsnow键   
  7. If Not Key2 Is Nothing Then   
  8. Key1.DeleteSubKey("northsnow")   
  9. '如果键不存在就创建它   
  10. End If  

3、创建或读取注册表项

  1. Dim Key1 As Microsoft.Win32.RegistryKey  
  2. Key1 = My.Computer.Registry.CurrentUser '返回当前用户键  
  3. Dim Key2 As Microsoft.Win32.RegistryKey  
  4. Key2 = Key1.OpenSubKey("northsnow", True) '返回当前用户键下的northsnow键,  
  5. 如果想创建项,必须指定第二个参数为true  
  6. If Key2 Is Nothing Then  
  7. Key2 = Key1.CreateSubKey("northsnow") '如果键不存在就创建它  
  8. End If  
  9.  
  10. '创建项,如果不存在就创建,如果存在则覆盖  
  11. Key2.SetValue("name", "塞北的雪")  
  12. Key2.SetValue("sex", True)  
  13. Key2.SetValue("age", 30)  
  14. '返回项值  
  15. Dim sb As New System.Text.StringBuilder  
  16. sb.AppendLine(Key2.GetValue("name"))  
  17. sb.AppendLine(Key2.GetValue("sex"))  
  18. sb.AppendLine(Key2.GetValue("age"))  
  19. MsgBox(sb.ToString)  
  20. '查验某个项是否存在  
  21. If (Key2.GetValue("name")) Is Nothing Then  
  22. MsgBox("no")  
  23. Else  
  24. MsgBox("yes")  
  25. End If  
  26.  
  27. If (Key2.GetValue("name2")) Is Nothing Then  
  28. MsgBox("no")  
  29. Else  
  30. MsgBox("yes")  
  31. End If  
  32.  
  33. '输出  
  34. ' 塞北的雪  
  35. 'True  
  36. '30  
  37. 'yes  
  38. 'no 

4、遍历注册表

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

  1. Dim sb As New System.Text.StringBuilder   
  2. '返回遍历结果  
  3. Dim sb2 As New System.Text.StringBuilder   
  4. '返回读取出错的注册表键  
  5. Private Sub Button3_Click()Sub Button3_Click(ByVal sender As System.Object,  
  6. ByVal e As System.EventArgs) Handles Button3.Click  
  7. Dim Key1 As Microsoft.Win32.RegistryKey  
  8. Key1 = My.Computer.Registry.CurrentUser   
  9. '返回当前用户键  
  10. If Not Key1 Is Nothing Then  
  11. sb.AppendLine(Key1.Name)  
  12. readValue(Key1)  
  13. readReg(Key1)  
  14. End If  
  15. Me.TextBox1.Text = sb.ToString  
  16. Me.TextBox2.Text = sb2.ToString  
  17. End Sub  
  18.  
  19. '遍历注册表键树  
  20. Private Sub readReg()Sub readReg(ByVal r As Microsoft.Win32.RegistryKey)  
  21. If r.SubKeyCount > 0 Then  
  22. Dim keyName() As String  
  23. Dim keyTemp As Microsoft.Win32.RegistryKey  
  24. keyName = r.GetSubKeyNames  
  25. Dim i As Integer  
  26. For i = 0 To keyName.GetLength(0) - 1  
  27. Try  
  28. sb.AppendLine(keyName(i))  
  29. keyTemp = r.OpenSubKey(keyName(i), True)  
  30. readValue(keyTemp)  
  31. readReg(keyTemp)  
  32. Catch ex As Exception  
  33. sb2.AppendLine(keyName(i))  
  34. End Try  
  35. Next  
  36. End If  
  37. End Sub  
  38.  
  39. '遍历某键下的项  
  40. Private Sub readValue()Sub readValue(ByVal r As Microsoft.Win32.RegistryKey)  
  41. If r.ValueCount > 0 Then  
  42. Dim valueName() As String  
  43. Dim i As Integer  
  44. valueName = r.GetValueNames  
  45. For i = 0 To valueName.GetLength(0) - 1  
  46. sb.AppendLine("####")  
  47. sb.Append(r.Name)  
  48. sb.Append("----")  
  49. sb.Append(r.GetValue(valueName(i)).ToString)  
  50. Next  
  51. End If  
  52. End Sub 

【编辑推荐】

  1. 浅析VB.NET开发自动分页
  2. VB.NET ListView控件经验总结
  3. 简单讲述VB.NET读取INI
  4. 简要介绍VB System.Array类及其成员
  5. 概括VB.NET获取网卡地址的步骤
责任编辑:佚名 来源: IT168
相关推荐

2009-10-12 16:08:14

VB.NET访问注册表

2010-01-11 18:40:03

VB.NET操作注册表

2010-01-08 10:09:50

VB.NET注册表操作

2009-11-10 17:31:38

VB.NET注册表

2009-10-26 14:50:18

VB.NET遍历注册表

2009-10-26 13:46:31

VB.NET注册表权限

2010-01-18 13:57:38

VB.NET读写注册表

2009-10-29 17:33:51

VB.NET线程方法

2009-10-20 10:16:24

VB.NET COMB

2010-01-11 16:04:10

VB.NET使用wit

2010-01-12 16:30:21

VB.NET数据转换

2010-01-18 18:20:49

VB.NET使用API

2009-10-14 17:21:47

VB.NET定制Win

2009-10-13 14:42:30

VB.NET静态成员

2009-10-12 13:54:22

VB.NET Data

2009-10-23 13:10:14

VB.NET List

2009-10-15 11:42:05

VB.Net赋值语句

2009-10-10 17:06:09

VB和VB.NET

2009-10-13 14:38:10

VB.NET访问类型

2009-10-12 16:56:36

VB.NET常量VB.NET枚举
点赞
收藏

51CTO技术栈公众号