91在线一级黄片|91视频在线观看18|成人夜间呦呦网站|91资源欧美日韩超碰|久久最新免费精品视频一区二区三区|国产探花视频在线观看|黄片真人免费三级片毛片|国产人无码视频在线|精品成人影视无码三区|久久视频爱久久免费精品

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
實例講解VB.NET訪問注冊表

在向大家詳細介紹VB.NET訪問注冊表之前,首先讓大家了解下registry類和registryKey類,然后全面介紹VB.NET訪問注冊表。

VB.NET訪問注冊表非常的簡單。我們可以用microsoft.Win32 名稱空間的下的registry類和registryKey類。另外My.Computer.Registry 也可以返回一個Microsoft.Win32.Registry類的實例。

下面就舉幾個小例子來說明VB.NET訪問注冊表的方法。

1.返回或創(chuàng)建一個注冊表鍵

 
 
 
  1. Dim Key1 As Microsoft.Win32.RegistryKey 
  2. Key1 = My.Computer.Registry.CurrentUser '返回當(dāng)前用戶鍵 
  3. Dim Key2 As Microsoft.Win32.RegistryKey 
  4. Key2 = Key1.OpenSubKey("northsnow") '返回當(dāng)前用戶鍵下的northsnow鍵 
  5. If Key2 Is Nothing Then 
  6. Key2 = Key1.CreateSubKey("northsnow") '如果鍵不存在就創(chuàng)建它 
  7. End If 

2.刪除注冊表鍵

 
 
 
  1. Dim Key1 As Microsoft.Win32.RegistryKey 
  2. Key1 = My.Computer.Registry.CurrentUser '返回當(dāng)前用戶鍵 
  3. Dim Key2 As Microsoft.Win32.RegistryKey 
  4. Key2 = Key1.OpenSubKey("northsnow") '返回當(dāng)前用戶鍵下的northsnow鍵 
  5. If Not Key2 Is Nothing Then 
  6. Key1.DeleteSubKey("northsnow") '如果鍵不存在就創(chuàng)建它 
  7. End If 

3.創(chuàng)建或讀取注冊表項

 
 
 
  1. Dim Key1 As Microsoft.Win32.RegistryKey
  2. Key1 = My.Computer.Registry.CurrentUser '返回當(dāng)前用戶鍵
  3. Dim Key2 As Microsoft.Win32.RegistryKey
  4. Key2 = Key1.OpenSubKey("northsnow", True) '返回當(dāng)前用戶鍵下的northsnow鍵,
  5. 如果想創(chuàng)建項,必須指定第二個參數(shù)為true
  6. If Key2 Is Nothing Then
  7. Key2 = Key1.CreateSubKey("northsnow") '如果鍵不存在就創(chuàng)建它
  8. End If
  9. '創(chuàng)建項,如果不存在就創(chuàng)建,如果存在則覆蓋
  10. Key2.SetValue("name", "塞北的雪")
  11. Key2.SetValue("sex", True)
  12. Key2.SetValue("age", 30)
  13. '返回項值
  14. Dim sb As New System.Text.StringBuilder
  15. sb.AppendLine(Key2.GetValue("name"))
  16. sb.AppendLine(Key2.GetValue("sex"))
  17. sb.AppendLine(Key2.GetValue("age"))
  18. MsgBox(sb.ToString)
  19. '查驗?zāi)硞€項是否存在
  20. If (Key2.GetValue("name")) Is Nothing Then
  21. MsgBox("no")
  22. Else
  23. MsgBox("yes")
  24. End If
  25. If (Key2.GetValue("name2")) Is Nothing Then
  26. MsgBox("no")
  27. Else
  28. MsgBox("yes")
  29. End If

4.遍歷注冊表

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

以上介紹VB.NET訪問注冊表。


當(dāng)前名稱:實例講解VB.NET訪問注冊表
新聞來源:http://m.jiaoqi3.com/article/coecege.html