新聞中心
大家通過(guò)對(duì)VB.NET的學(xué)習(xí),可以知道,這款編程語(yǔ)言的應(yīng)用范圍是非常廣泛的。下面就一起來(lái)分析一下VB.NET注冊(cè)表操作的一些技巧。其實(shí),VB.NET注冊(cè)表操作是非常的簡(jiǎn)單。我們可以用 microsoft.Win32 名稱(chēng)空間的 下的 registry 類(lèi) 和 registryKey 類(lèi)?!×硗狻y.Computer.Registry 也可以返回一個(gè) Microsoft.Win32.Registry 類(lèi)的實(shí)例。 #t#

創(chuàng)新互聯(lián)建站于2013年開(kāi)始,先為南漳等服務(wù)建站,南漳等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢(xún)服務(wù)。為南漳企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
下面就舉幾個(gè)小例子來(lái)說(shuō)明VB.NET注冊(cè)表操作的方法。
VB.NET注冊(cè)表操作1,返回或創(chuàng)建一個(gè)注冊(cè)表鍵
- Dim Key1 As Microsoft.Win32.
RegistryKey - Key1 = My.Computer.Registry.
CurrentUser '返回當(dāng)前用戶(hù)鍵 - Dim Key2 As Microsoft.Win32.
RegistryKey - Key2 = Key1.OpenSubKey("northsnow")
'返回當(dāng)前用戶(hù)鍵下的northsnow鍵 - If Key2 Is Nothing Then
- Key2 = Key1.CreateSubKey("northsnow")
'如果鍵不存在就創(chuàng)建它 - End If
VB.NET注冊(cè)表操作2,刪除注冊(cè)表鍵
- Dim Key1 As Microsoft.Win32.
RegistryKey- Key1 = My.Computer.Registry.
CurrentUser '返回當(dāng)前用戶(hù)鍵- Dim Key2 As Microsoft.Win32.
RegistryKey- Key2 = Key1.OpenSubKey("northsnow")
'返回當(dāng)前用戶(hù)鍵下的northsnow鍵- If Not Key2 Is Nothing Then
- Key1.DeleteSubKey("northsnow")
'如果鍵不存在就創(chuàng)建它- End If
VB.NET注冊(cè)表操作3,創(chuàng)建或讀取注冊(cè)表項(xiàng)
- Dim Key1 As Microsoft.Win32.RegistryKey
- Key1 = My.Computer.Registry.CurrentUser
'返回當(dāng)前用戶(hù)鍵- Dim Key2 As Microsoft.Win32.RegistryKey
- Key2 = Key1.OpenSubKey("northsnow",
True) '返回當(dāng)前用戶(hù)鍵下的northsnow
鍵,如果想創(chuàng)建項(xiàng),必須指定第二個(gè)參數(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)硞€(gè)項(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
VB.NET注冊(cè)表操作4,遍歷注冊(cè)表
這個(gè)也非常簡(jiǎn)單,在窗體上放一個(gè)按鈕和兩個(gè)文本框,添加如下的代碼
- Dim sb As New System.Text.StringBuilder
'返回遍歷結(jié)果- Dim sb2 As New System.Text.StringBuilder
'返回讀取出錯(cuò)的注冊(cè)表鍵- 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)前用戶(hù)鍵- 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
- '遍歷注冊(cè)表鍵樹(shù)
- 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
VB.NET注冊(cè)表操作的一些實(shí)際應(yīng)用技巧就為大家介紹到這里。
分享題目:VB.NET注冊(cè)表操作相關(guān)技巧解析
網(wǎng)頁(yè)地址:http://m.fisionsoft.com.cn/article/dpcpspp.html


咨詢(xún)
建站咨詢(xún)
