VB.NET中快速存取註冊表技巧

來源:互聯網
上載者:User

vb.net中訪問註冊表變得非常的簡單。我們可以用microsoft.Win32 名稱空間的下的registry類和registryKey類。另外My.Computer.Registry 也可以返回一個Microsoft.Win32.Registry類的執行個體。

下面就舉幾個小例子來說明vb.net訪問註冊表的方法。

1、返回或建立一個註冊表鍵

 

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") '如果鍵不存在就建立它

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") '如果鍵不存在就建立它

End If

 

3、建立或讀取登錄機碼

4、遍曆註冊表

Dim Key1 As Microsoft.Win32.RegistryKey

Key1 = My.Computer.Registry.CurrentUser '返回目前使用者鍵

Dim Key2 As Microsoft.Win32.RegistryKey

Key2 = Key1.OpenSubKey("northsnow", True) '返回目前使用者鍵下的northsnow鍵,

如果想建立項,必須指定第二個參數為true

If Key2 Is Nothing Then

Key2 = Key1.CreateSubKey("northsnow") '如果鍵不存在就建立它

End If

'建立項,如果不存在就建立,如果存在則覆蓋

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

'輸出

' 塞北的雪

'True

'30

'yes

'no

 

 

這個也非常簡單,在表單上放一個按鈕和兩個文字框,添加如下的代碼:

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.