日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

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

新聞中心

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

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

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

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

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

 
 
 
  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.刪除注冊(cè)表鍵

 
 
 
  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)建或讀取注冊(cè)表項(xià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)建項(xiàng),必須指定第二個(gè)參數(shù)為true
  6. If Key2 Is Nothing Then
  7. Key2 = Key1.CreateSubKey("northsnow") '如果鍵不存在就創(chuàng)建它
  8. End If
  9. '創(chuàng)建項(xiàng),如果不存在就創(chuàng)建,如果存在則覆蓋
  10. Key2.SetValue("name", "塞北的雪")
  11. Key2.SetValue("sex", True)
  12. Key2.SetValue("age", 30)
  13. '返回項(xiàng)值
  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. '查驗(yàn)?zāi)硞€(gè)項(xiàng)是否存在
  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.遍歷注冊(cè)表

 
 
 
  1. Dim sb As New System.Text.StringBuilder '返回遍歷結(jié)果
  2. Dim sb2 As New System.Text.StringBuilder '返回讀取出錯(cuò)的注冊(cè)表鍵
  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. '遍歷注冊(cè)表鍵樹
  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. '遍歷某鍵下的項(xiàng)
  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訪問注冊(cè)表。


名稱欄目:實(shí)例講解VB.NET訪問注冊(cè)表
標(biāo)題URL:http://www.dlmjj.cn/article/coecege.html