濮阳杆衣贸易有限公司

主頁 > 知識庫 > 在VB.NET中如何快速訪問注冊表的技巧 代碼

在VB.NET中如何快速訪問注冊表的技巧 代碼

熱門標(biāo)簽:地圖標(biāo)注用小紅旗 電話外呼系統(tǒng)線路哪家公司做得好 免費(fèi)高德地圖標(biāo)注 成都人工外呼系統(tǒng)平臺 索契地圖標(biāo)注app 外呼系統(tǒng)工作推進(jìn) 巴東單位怎么做地圖標(biāo)注 地圖標(biāo)注員項(xiàng)目 儋州電銷機(jī)器人廠家

vb.net中訪問注冊表變得非常的簡單。我們可以用microsoft.Win32 名稱空間的下的registry類和registryKey類。另外My.Computer.Registry 也可以返回一個Microsoft.Win32.Registry類的實(shí)例。
  下面就舉幾個小例子來說明vb.net訪問注冊表的方法。
 ?。?、返回或創(chuàng)建一個注冊表鍵

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

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

  Dim Key1 As Microsoft.Win32.RegistryKey
Key1 = My.Computer.Registry.CurrentUser '返回當(dāng)前用戶鍵
Dim Key2 As Microsoft.Win32.RegistryKey
Key2 = Key1.OpenSubKey("northsnow", True) '返回當(dāng)前用戶鍵下的northsnow鍵,

  如果想創(chuàng)建項(xiàng),必須指定第二個參數(shù)為true
If Key2 Is Nothing Then
Key2 = Key1.CreateSubKey("northsnow") '如果鍵不存在就創(chuàng)建它
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)
'查驗(yàn)?zāi)硞€項(xiàng)是否存在
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


4、遍歷注冊表
  這個也非常簡單,  在窗體上放一個按鈕和兩個文本框,添加如下的代碼:

  Dim sb As New System.Text.StringBuilder '返回遍歷結(jié)果
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 '返回當(dāng)前用戶鍵
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
'遍歷某鍵下的項(xiàng)
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

 

標(biāo)簽:安康 鶴壁 長春 鄂州 茂名 龍巖 儋州 江蘇

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《在VB.NET中如何快速訪問注冊表的技巧 代碼》,本文關(guān)鍵詞  在,VB.NET,中,如何,快速,訪問,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《在VB.NET中如何快速訪問注冊表的技巧 代碼》相關(guān)的同類信息!
  • 本頁收集關(guān)于在VB.NET中如何快速訪問注冊表的技巧 代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    平山县| 仁化县| 山西省| 临海市| 磴口县| 万全县| 始兴县| 高密市| 景宁| 天长市| 林口县| 杨浦区| 封丘县| 南郑县| 万源市| 日喀则市| 彭泽县| 伽师县| 贞丰县| 乐昌市| 台江县| 望城县| 吉林市| 蒙城县| 曲周县| 苏州市| 汪清县| 雅安市| 阿城市| 镇远县| 卢氏县| 吴江市| 鄂尔多斯市| 沂南县| 利川市| 河北区| 卫辉市| 新源县| 平果县| 醴陵市| 天等县|