温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎样完成VB.NET读写注册表

发布时间:2021-10-27 17:27:01 来源:亿速云 阅读:438 作者:柒染 栏目:编程语言

这篇文章将为大家详细讲解有关怎样完成VB.NET读写注册表,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

程序员在选择使用何种编程语言来帮助他们实现自己的程序开发的时候,首先考虑的因素就是实用,灵活,效率高的语言。而VB.NET就是这样一款比较适合的编程语言。比如,VB.NET读写注册表就变得非常的简单。我们可以用 microsoft.Win32 名称空间的 下的registry 类和registryKey类。 另外 My.Computer.Registry 也可以返回一个 Microsoft.Win32.Registry 类的实例。

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

VB.NET读写注册表1,返回或创建一个注册表键

  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") 
    '返回当前用户键下的northsnow键  

  5. If Key2 Is Nothing Then  

  6. Key2 = Key1.CreateSubKey("northsnow") 
    '如果键不存在就创建它  

  7. End If 

VB.NET读写注册表2,删除注册表键

  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")
     '返回当前用户键下的northsnow键  

  5. If Not Key2 Is Nothing Then  

  6. Key1.DeleteSubKey("northsnow") 
    '如果键不存在就创建它  

  7. End If 

VB.NET读写注册表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键,如果想创建项,
    必须指定第二个参数为true  

  5. If Key2 Is Nothing Then  

  6. Key2 = Key1.CreateSubKey("northsnow") 
    '如果键不存在就创建它  

  7. End If  

  8. '创建项,如果不存在就创建,如果存在则覆盖  

  9. Key2.SetValue("name", "塞北的雪")  

  10. Key2.SetValue("sex", True)  

  11. Key2.SetValue("age", 30)  

  12. '返回项值  

  13. Dim sb As New System.Text.StringBuilder  

  14. sb.AppendLine(Key2.GetValue("name"))  

  15. sb.AppendLine(Key2.GetValue("sex"))  

  16. sb.AppendLine(Key2.GetValue("age"))  

  17. MsgBox(sb.ToString)  

  18. '查验某个项是否存在  

  19. If (Key2.GetValue("name")) Is Nothing Then  

  20. MsgBox("no")  

  21. Else  

  22. MsgBox("yes")  

  23. End If  

  24. If (Key2.GetValue("name2")) Is Nothing Then  

  25. MsgBox("no")  

  26. Else  

  27. MsgBox("yes")  

  28. End If  

  29. '输出   

  30. ' 塞北的雪  

  31. 'True  

  32. '30  

  33. 'yes  

  34. 'no 

VB.NET读写注册表4,遍历注册表

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

  1. Dim sb As New System.Text.StringBuilder 
    '返回遍历结果  

  2. Dim sb2 As New System.Text.StringBuilder 
    '返回读取出错的注册表键  

  3. Private Sub Button3_Click()Sub Button3_
    Click(ByVal sender As System.Object, 
    ByVal e As System.EventArgs) Handles 
    Button3.Click  

  4. Dim Key1 As Microsoft.Win32.RegistryKey  

  5. Key1 = My.Computer.Registry.CurrentUser 
    '返回当前用户键  

  6. If Not Key1 Is Nothing Then  

  7. sb.AppendLine(Key1.Name)  

  8. readValue(Key1)  

  9. readReg(Key1)  

  10. End If  

  11. Me.TextBox1.Text = sb.ToString  

  12. Me.TextBox2.Text = sb2.ToString  

  13. End Sub  

  14. '遍历注册表键树  

  15. Private Sub readReg()Sub readReg(ByVal 
    r As Microsoft.Win32.RegistryKey)  

  16. If r.SubKeyCount > 0 Then  

  17. Dim keyName() As String  

  18. Dim keyTemp As Microsoft.Win32.RegistryKey  

  19. keyName = r.GetSubKeyNames  

  20. Dim i As Integer  

  21. For i = 0 To keyName.GetLength(0) - 1  

  22. Try  

  23. sb.AppendLine(keyName(i))  

  24. keyTemp = r.OpenSubKey(keyName(i), True)  

  25. readValue(keyTemp)  

  26. readReg(keyTemp)  

  27. Catch ex As Exception  

  28. sb2.AppendLine(keyName(i))  

  29. End Try  

  30. Next  

  31. End If  

  32. End Sub  

  33. '遍历某键下的项  

  34. Private Sub readValue()Sub readValue(ByVal
     r As Microsoft.Win32.RegistryKey)  

  35. If r.ValueCount > 0 Then  

  36. Dim valueName() As String  

  37. Dim i As Integer  

  38. valueName = r.GetValueNames  

  39. For i = 0 To valueName.GetLength(0) - 1  

  40. sb.AppendLine("####")  

  41. sb.Append(r.Name)  

  42. sb.Append("----")  

  43. sb.Append(r.GetValue(valueName(i)).ToString)  

  44. Next  

  45. End If  

  46. End Sub 

关于怎样完成VB.NET读写注册表就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI