日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
講解VB.NET訪問注冊表方法

本文向大家介紹VB.NET訪問注冊表,可能好多人還不了解VB.NET訪問注冊表,沒有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會你更多東西。

目前創(chuàng)新互聯(lián)已為上1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站托管運營、企業(yè)網(wǎng)站設(shè)計、霸州網(wǎng)站維護等服務(wù),公司將堅持客戶導向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

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

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

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

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

2、刪除注冊表鍵

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

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

 
 
 
  1. Dim Key1 As Microsoft.Win32.RegistryKey  
  2. Key1 = My.Computer.Registry.CurrentUser '返回當前用戶鍵  
  3. Dim Key2 As Microsoft.Win32.RegistryKey  
  4. Key2 = Key1.OpenSubKey("northsnow", True) '返回當前用戶鍵下的northsnow鍵,  
  5. 如果想創(chuàng)建項,必須指定第二個參數(shù)為true  
  6. If Key2 Is Nothing Then  
  7. Key2 = Key1.CreateSubKey("northsnow") '如果鍵不存在就創(chuàng)建它  
  8. End If  
  9.  
  10. '創(chuàng)建項,如果不存在就創(chuàng)建,如果存在則覆蓋  
  11. Key2.SetValue("name", "塞北的雪")  
  12. Key2.SetValue("sex", True)  
  13. Key2.SetValue("age", 30)  
  14. '返回項值  
  15. Dim sb As New System.Text.StringBuilder  
  16. sb.AppendLine(Key2.GetValue("name"))  
  17. sb.AppendLine(Key2.GetValue("sex"))  
  18. sb.AppendLine(Key2.GetValue("age"))  
  19. MsgBox(sb.ToString)  
  20. '查驗?zāi)硞€項是否存在  
  21. If (Key2.GetValue("name")) Is Nothing Then  
  22. MsgBox("no")  
  23. Else  
  24. MsgBox("yes")  
  25. End If  
  26.  
  27. If (Key2.GetValue("name2")) Is Nothing Then  
  28. MsgBox("no")  
  29. Else  
  30. MsgBox("yes")  
  31. End If  
  32.  
  33. '輸出  
  34. ' 塞北的雪  
  35. 'True  
  36. '30  
  37. 'yes  
  38. 'no 

4、遍歷注冊表

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

 
 
 
  1. Dim sb As New System.Text.StringBuilder   
  2. '返回遍歷結(jié)果  
  3. Dim sb2 As New System.Text.StringBuilder   
  4. '返回讀取出錯的注冊表鍵  
  5. Private Sub Button3_Click()Sub Button3_Click(ByVal sender As System.Object,  
  6. ByVal e As System.EventArgs) Handles Button3.Click  
  7. Dim Key1 As Microsoft.Win32.RegistryKey  
  8. Key1 = My.Computer.Registry.CurrentUser   
  9. '返回當前用戶鍵  
  10. If Not Key1 Is Nothing Then  
  11. sb.AppendLine(Key1.Name)  
  12. readValue(Key1)  
  13. readReg(Key1)  
  14. End If  
  15. Me.TextBox1.Text = sb.ToString  
  16. Me.TextBox2.Text = sb2.ToString  
  17. End Sub  
  18.  
  19. '遍歷注冊表鍵樹  
  20. Private Sub readReg()Sub readReg(ByVal r As Microsoft.Win32.RegistryKey)  
  21. If r.SubKeyCount > 0 Then  
  22. Dim keyName() As String  
  23. Dim keyTemp As Microsoft.Win32.RegistryKey  
  24. keyName = r.GetSubKeyNames  
  25. Dim i As Integer  
  26. For i = 0 To keyName.GetLength(0) - 1  
  27. Try  
  28. sb.AppendLine(keyName(i))  
  29. keyTemp = r.OpenSubKey(keyName(i), True)  
  30. readValue(keyTemp)  
  31. readReg(keyTemp)  
  32. Catch ex As Exception  
  33. sb2.AppendLine(keyName(i))  
  34. End Try  
  35. Next  
  36. End If  
  37. End Sub  
  38.  
  39. '遍歷某鍵下的項  
  40. Private Sub readValue()Sub readValue(ByVal r As Microsoft.Win32.RegistryKey)  
  41. If r.ValueCount > 0 Then  
  42. Dim valueName() As String  
  43. Dim i As Integer  
  44. valueName = r.GetValueNames  
  45. For i = 0 To valueName.GetLength(0) - 1  
  46. sb.AppendLine("####")  
  47. sb.Append(r.Name)  
  48. sb.Append("----")  
  49. sb.Append(r.GetValue(valueName(i)).ToString)  
  50. Next  
  51. End If  
  52. End Sub 

【編輯推薦】

  1. 淺析VB.NET開發(fā)自動分頁
  2. VB.NET ListView控件經(jīng)驗總結(jié)
  3. 簡單講述VB.NET讀取INI
  4. 簡要介紹VB System.Array類及其成員
  5. 概括VB.NET獲取網(wǎng)卡地址的步驟

當前名稱:講解VB.NET訪問注冊表方法
URL標題:http://m.5511xx.com/article/dhchdjj.html