明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

走近VB.Net(12) 注冊表迅速基礎(chǔ)

[摘要]走近VB.Net(十二) 注冊表快速入門 作者:hejianzhong VB.Net中文站(http://vbnetcn.126.com) 首先介紹一下...
走近VB.Net(十二) 注冊表快速入門
作者:hejianzhong VB.Net中文站(http://vbnetcn.126.com)
首先介紹一下注冊表的相關(guān)基礎(chǔ)知識:注冊表的每一個文件夾被稱為一個Key(項),這個文件夾的子文件夾被稱為SubKey(子
項),而在一個子項中有不同的Value Name(值項—即數(shù)值頂),值項后面就是你要保存的數(shù)據(jù)Value Data(數(shù)據(jù))了。而在子項中
通常都有一個Default Value 是默認的Value Name,相信打開過注冊表的一定看得很清楚:
好了,我們現(xiàn)在看一下如何寫入注冊表。
最易的方法就是使用VB內(nèi)置的函數(shù)了,這與VB6中使用的方法一模一樣:
————————————————————————————————————————
設(shè)置Sub SaveSetting(AppName As String,Section As String,Key As String,Setting As String)
獲取 Function GetSetting(AppName As String,Section As String,Key As String,[Default As String])
刪除DeleteSetting(AppName As String,[Section As String=nothing],[Key As String=nothing] )
————————————————————————————————————————————————
不過他只能寫在一個固定的位置,寫入HKEY_CURRENT_USER//Software//VB and VBA Program Setting.當(dāng)我們需要使
用注冊表加密時,總不能寫在這樣一個大家都知道的地方吧?還有當(dāng)我們要使用注冊表實現(xiàn)一些功能時(如寫入run主鍵讓程序
啟動時自運行)這個更是無能為力。相信大家都看過VB6的例程,很是復(fù)雜。不過我看的一個系列文章寫得很好(在我的網(wǎng)站
---VB6知識庫中有收錄)竟有七八頁。在VB.Net中就很簡單了,跟上面真有些差不多。--------(不過我可是研究了很久的,很辛
苦---最近有人把我的文章改成自已的名字,我很傷心,也請這些人自重,再次聲明在任何地方張貼必須經(jīng)過我的同意,并明確
標(biāo)明:“作者:hejianzhong VB.Net中文站 http:://vbnetcn.126.com.”的字樣)
我在這里寫了一個模塊,只是為了示例,大家在使用的時候記住不需要像我這樣另外使用模塊(應(yīng)該根據(jù)你的需要靈活運
用),這有些畫蛇添足。這個我也不打算提供源碼下載,因為實在太簡單了。
首先如圖添加控件:


textbox控件的text設(shè)為“”,其它的請作相應(yīng)的修改,實際上這個示例你不編也可以,只要看懂下面的內(nèi)容就行了:
寫代碼如下:

Imports System.ComponentModel Imports System.Drawing Imports System.WinForms Public Class Form1 Inherits System.WinForms.Form Public Sub New() MyBase.New Form1 = Me 'This call is required by the Win Form Designer. InitializeComponent 'TODO: Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Overrides Public Sub Dispose() MyBase.Dispose components.Dispose End Sub 下面這些 #Region " Windows Form Designer generated code " 'Required by the Windows Form Designer Private components As System.ComponentModel.Container Private WithEvents Label2 As System.WinForms.Label Private WithEvents Label1 As System.WinForms.Label Private WithEvents TextBox2 As System.WinForms.TextBox Private WithEvents Button4 As System.WinForms.Button Private WithEvents Button3 As System.WinForms.Button Private WithEvents TextBox1 As System.WinForms.TextBox Private WithEvents Button1 As System.WinForms.Button Private WithEvents RadioButton2 As System.WinForms.RadioButton Private WithEvents RadioButton1 As System.WinForms.RadioButton Dim WithEvents Form1 As System.WinForms.Form 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Private Sub InitializeComponent() Me.components = New System.ComponentModel.Container() Me.TextBox1 = New System.WinForms.TextBox() Me.TextBox2 = New System.WinForms.TextBox() Me.Label2 = New System.WinForms.Label() Me.Label1 = New System.WinForms.Label() Me.Button4 = New System.WinForms.Button() Me.RadioButton1 = New System.WinForms.RadioButton() Me.Button3 = New System.WinForms.Button() Me.Button1 = New System.WinForms.Button() Me.RadioButton2 = New System.WinForms.RadioButton() '@design Me.TrayHeight = 0 '@design Me.TrayLargeIcon = False '@design Me.TrayAutoArrange = True TextBox1.Location = New System.Drawing.Point(184, 80) TextBox1.Multiline = True TextBox1.TabIndex = 3 TextBox1.Size = New System.Drawing.Size(144, 24) TextBox2.Location = New System.Drawing.Point(184, 128) TextBox2.Multiline = True TextBox2.TabIndex = 7 TextBox2.Size = New System.Drawing.Size(144, 24) Label2.Location = New System.Drawing.Point(64, 192) Label2.Text = "Label2" Label2.Size = New System.Drawing.Size(264, 32) Label2.TabIndex = 9 Label1.Location = New System.Drawing.Point(16, 184) Label1.Text = "當(dāng)前位置" Label1.Size = New System.Drawing.Size(40, 32) Label1.TabIndex = 8 Button4.Location = New System.Drawing.Point(8, 152) Button4.Size = New System.Drawing.Size(168, 24) Button4.TabIndex = 6 Button4.Text = "讀取數(shù)據(jù)" RadioButton1.Location = New System.Drawing.Point(8, 8) RadioButton1.Text = "寫入HKEY_CURRENT_USER " RadioButton1.Size = New System.Drawing.Size(168, 24) RadioButton1.TabIndex = 0 Button3.Location = New System.Drawing.Point(8, 112) Button3.Size = New System.Drawing.Size(168, 24) Button3.TabIndex = 5 Button3.Text = "寫入數(shù)據(jù)" Button1.Location = New System.Drawing.Point(8, 80) Button1.Size = New System.Drawing.Size(168, 24) Button1.TabIndex = 2 Button1.Text = "打開注冊表子項(subkey)" RadioButton2.Location = New System.Drawing.Point(8, 40) RadioButton2.Text = "寫入HKEY_LOCAL_MACHINE " RadioButton2.Size = New System.Drawing.Size(168, 40) RadioButton2.TabIndex = 1 Me.Text = "Form1" Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14) Me.ClientSize = New System.Drawing.Size(336, 277) Me.Controls.Add(Label2) Me.Controls.Add(Label1) Me.Controls.Add(TextBox2) Me.Controls.Add(Button4) Me.Controls.Add(Button3) Me.Controls.Add(TextBox1) Me.Controls.Add(Button1) Me.Controls.Add(RadioButton2) Me.Controls.Add(RadioButton1) End Sub #End Region Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) textbox2.Text = getval("數(shù)值項").ToString End Sub Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) writeval("數(shù)值項", textbox2.Text) : textbox2.Text = "已寫入到注冊表" End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) If objkey = Nothing Then msgbox("請先在上面選定注冊表根項") Me.RadioButton1.Select() OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser) label2.Text = objkey.ToString Exit Sub End If newsubkey(textbox1.Text) label2.Text = objkey.ToString End Sub Protected Sub RadioButton2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) openbasekey(Microsoft.Win32.RegistryHive.LocalMachine) label2.Text = objkey.ToString End Sub Protected Sub RadioButton1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser) label2.Text = objkey.ToString End Sub End Class '################################################################################################################## Public Module RWregKey '定義一個objKey 的注冊項對象 Public objKey As Microsoft.Win32.RegistryKey '_________________________________________________________________________________________________________________ '使用openremotebasekey方法打開一個注冊表根項目,使用rgistryhive獲取一個最上層的Key 根項目 Public Sub OpenBaseKey(ByVal basekey As Microsoft.Win32.RegistryHive) objkey = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(basekey, SystemInformation.ComputerName) End Sub '__________________________________________________________________________________________________ '使opensubkey打開一個指定的子項目, Public Sub NextSubKey(ByVal keyname As String, ByVal writeable As Boolean) objkey = objkey.OpenSubKey(keyname, writeable) '后面的布爾值(True,false)指定是否可讀寫 If objkey = keytemp Then End Sub '____________________________________________________________________________________________________________ '_______________________________________________________________________________________________________ '如果找不到就創(chuàng)造一個新的subkey子項目 Public Sub NewSubKey(ByVal keyname As String) objkey = objKey.CreateSubKey(keyname) '在這里我們創(chuàng)造了一個新的subkey(項) End Sub '____________________________________________________________________________________________________________________ '寫入需要保存的數(shù)據(jù)-------ValueData Public Sub writeVal(ByVal valname As String, ByVal valdata As Object) objkey.SetValue(valname, valdata) '寫入數(shù)據(jù)----ValueData End Sub ' ____________________________________________________________________ '讀取保存的數(shù)據(jù) Public Function getval(ByVal valname As String) As Object Return objKey.GetValue(valname) End Function '_________ '刪除一個注冊表項 Public Sub delKey(ByVal keyname As String) objkey.DeleteSubKey(keyname) End Sub '___________________________________________________________________________________________________________() '____________________________________________________________________________________________________________________ '刪除一個數(shù)據(jù)------ValueData Public Sub delval(ByVal valname As String) objkey.DeleteValue(valname) '刪除了一個值 objkey.Close() '記住在使用過后把他關(guān)閉,這通常是被提倡的好習(xí)慣。 End Sub End Module 大家看了是不是很簡單,我曾在論壇看到人說,VB6用得很好我為什么要用VB.Net.而且Net只是Net技術(shù),我們的這個……..不相關(guān)。這 真是天大的誤解。就因為后面帶了一個net,大家都不理解并疏遠他,不論怎么樣,VB.Net因為面向?qū)ο罂梢愿行实膶懗绦。大家為什么要拒絕呢? 我的系列文單出來以后,學(xué)VB.Net的人也增加了不少,我在網(wǎng)易論壇看到一個網(wǎng)友說為了裝VB.Net裝了一下午沒裝上,后來我告訴他到我的網(wǎng)站立馬搞定了。 所以我并不反對張貼我的文章(我自已在討論組宣傳VB.Net又有打廣告的嫌疑),可是你總要跟我打個招呼吧?我沒有私心,一心為大家,可你們?yōu)槭裁匆?把我的名字與網(wǎng)址隱藏起來呢?更有甚者,竟公然把文章作者換成自已的名字(在網(wǎng)易我的《VB.Net圖解入門》《一步一步安裝VB.Net》竟被改成"作者:大頭鬼",---已被管理員刪了!,)?吹竭@些,我的心里有點涼。前幾天因為網(wǎng)速太慢,我通宵申請了一個高 速的網(wǎng)站,連夜上傳所有資料,誰知上傳又奇慢,兩天,在網(wǎng)上呆了十幾個小時。希望大家不要再這樣的打擊我了 ,這不是什么名利的問題,而是一個人起碼的尊嚴被杜