溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

VB.NET中怎么實(shí)現(xiàn)注冊(cè)表操作

發(fā)布時(shí)間:2021-07-15 11:32:19 來源:億速云 閱讀:182 作者:Leah 欄目:編程語(yǔ)言

這篇文章給大家介紹VB.NET中怎么實(shí)現(xiàn)注冊(cè)表操作,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

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  

VB.NET注冊(cè)表操作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  

VB.NET注冊(cè)表操作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
    鍵,如果想創(chuàng)建項(xiàng),必須指定第二個(gè)參數(shù)為true   

  5. If Key2 Is Nothing Then   

  6. Key2 = Key1.CreateSubKey("northsnow") 
    '如果鍵不存在就創(chuàng)建它   

  7. End If  

'創(chuàng)建項(xiàng),如果不存在就創(chuàng)建,如果存在則覆蓋   Key2.SetValue("name", "塞北的雪")   Key2.SetValue("sex", True)   Key2.SetValue("age", 30)
'返回項(xiàng)值   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)
  1. '查驗(yàn)?zāi)硞€(gè)項(xiàng)是否存在   

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

  3. MsgBox("no")   

  4. Else   

  5. MsgBox("yes")   

  6. End If  

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

  2. MsgBox("no")   

  3. Else   

  4. MsgBox("yes")   

  5. End If   

  6. '輸出   

  7. ' 塞北的雪   

  8. 'True   

  9. '30   

  10. 'yes   

  11. 'no  

VB.NET注冊(cè)表操作4,遍歷注冊(cè)表

這個(gè)也非常簡(jiǎn)單,在窗體上放一個(gè)按鈕和兩個(gè)文本框,添加如下的代碼

  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, 
    ByVal e As System.EventArgs) Handles 
    Button3.Click   

  4. Dim Key1 As Microsoft.Win32.RegistryKey   

  5. Key1 = My.Computer.Registry.CurrentUser 
    '返回當(dāng)前用戶鍵   

  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. '遍歷注冊(cè)表鍵樹   

  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. '遍歷某鍵下的項(xiàng)   

  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 

關(guān)于VB.NET中怎么實(shí)現(xiàn)注冊(cè)表操作就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI