這篇文章給大家介紹VB.NET中怎么實現(xiàn)注冊表操作,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
成都創(chuàng)新互聯(lián)公司主要從事成都做網(wǎng)站、網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)延邊朝鮮族,十余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792
VB.NET注冊表操作1,返回或創(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
VB.NET注冊表操作2,刪除注冊表鍵
Dim Key1 As Microsoft.Win32.
RegistryKeyKey1 = My.Computer.Registry.
CurrentUser '返回當(dāng)前用戶鍵Dim Key2 As Microsoft.Win32.
RegistryKeyKey2 = Key1.OpenSubKey("northsnow")
'返回當(dāng)前用戶鍵下的northsnow鍵If Not Key2 Is Nothing Then
Key1.DeleteSubKey("northsnow")
'如果鍵不存在就創(chuàng)建它End If
VB.NET注冊表操作3,創(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",
True) '返回當(dāng)前用戶鍵下的northsnow
鍵,如果想創(chuàng)建項,必須指定第二個參數(shù)為trueIf 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)
'查驗?zāi)硞€項是否存在
If (Key2.GetValue("name"))
Is Nothing ThenMsgBox("no")
Else
MsgBox("yes")
End If
If (Key2.GetValue("name2"))
Is Nothing ThenMsgBox("no")
Else
MsgBox("yes")
End If
'輸出
' 塞北的雪
'True
'30
'yes
'no
VB.NET注冊表操作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.ClickDim 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
'遍歷某鍵下的項
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
關(guān)于VB.NET中怎么實現(xiàn)注冊表操作就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網(wǎng)頁標(biāo)題:VB.NET中怎么實現(xiàn)注冊表操作
網(wǎng)頁網(wǎng)址:http://www.rwnh.cn/article2/jjseoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、動態(tài)網(wǎng)站、網(wǎng)站營銷、網(wǎng)站制作、電子商務(wù)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)