溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

VB.NET如何訪問注冊表

發(fā)布時間:2021-12-02 09:46:44 來源:億速云 閱讀:214 作者:小新 欄目:編程語言

這篇文章主要介紹了VB.NET如何訪問注冊表,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

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

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

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

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

2.刪除注冊表鍵

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

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

Dim Key1 As Microsoft.Win32.RegistryKey  Key1 = My.Computer.Registry.CurrentUser '返回當前用戶鍵  Dim Key2 As Microsoft.Win32.RegistryKey  Key2 = Key1.OpenSubKey("northsnow", True) '返回當前用戶鍵下的northsnow鍵,   如果想創(chuàng)建項,必須指定第二個參數(shù)為true  If Key2 Is Nothing Then  Key2 = Key1.CreateSubKey("northsnow") '如果鍵不存在就創(chuàng)建它  End If  '創(chuàng)建項,如果不存在就創(chuàng)建,如果存在則覆蓋  Key2.SetValue("name", "塞北的雪")  Key2.SetValue("sex", True)  Key2.SetValue("age", 30)  '返回項值  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)  '查驗某個項是否存在  If (Key2.GetValue("name")) Is Nothing Then  MsgBox("no")  Else  MsgBox("yes")  End If  If (Key2.GetValue("name2")) Is Nothing Then  MsgBox("no")  Else  MsgBox("yes")  End If

4.遍歷注冊表

Dim sb As New System.Text.StringBuilder '返回遍歷結果  Dim sb2 As New System.Text.StringBuilder '返回讀取出錯的注冊表鍵  Private Sub Button3_Click()Sub Button3_Click(ByVal sender As System.Object,   ByVal e As System.EventArgs) Handles Button3.Click  Dim Key1 As Microsoft.Win32.RegistryKey  Key1 = My.Computer.Registry.CurrentUser '返回當前用戶鍵  If Not Key1 Is Nothing Then  sb.AppendLine(Key1.Name)  readValue(Key1)  readReg(Key1)  End If  Me.TextBox1.Text = sb.ToString  Me.TextBox2.Text = sb2.ToString  End Sub  '遍歷注冊表鍵樹  Private Sub readReg()Sub readReg(ByVal r As Microsoft.Win32.RegistryKey)  If r.SubKeyCount > 0 Then  Dim keyName() As String  Dim keyTemp As Microsoft.Win32.RegistryKey  keyName = r.GetSubKeyNames  Dim i As Integer  For i = 0 To keyName.GetLength(0) - 1  Try  sb.AppendLine(keyName(i))  keyTemp = r.OpenSubKey(keyName(i), True)  readValue(keyTemp)  readReg(keyTemp)  Catch ex As Exception  sb2.AppendLine(keyName(i))  End Try  Next  End If  End Sub  '遍歷某鍵下的項  Private Sub readValue()Sub readValue(ByVal r As Microsoft.Win32.RegistryKey)  If r.ValueCount > 0 Then  Dim valueName() As String  Dim i As Integer  valueName = r.GetValueNames  For i = 0 To valueName.GetLength(0) - 1  sb.AppendLine("####")  sb.Append(r.Name)  sb.Append("----")  sb.Append(r.GetValue(valueName(i)).ToString)  Next  End If  End Sub

感謝你能夠認真閱讀完這篇文章,希望小編分享的“VB.NET如何訪問注冊表”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,更多相關知識等著你來學習!

向AI問一下細節(jié)

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

AI