走近VB.Net(12) 注冊(cè)表迅速基礎(chǔ)
發(fā)表時(shí)間:2023-04-07 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]走近VB.Net(十二) 注冊(cè)表快速入門 作者:hejianzhong VB.Net中文站(http://vbnetcn.126.com) 首先介紹一下...
走近VB.Net(十二) 注冊(cè)表快速入門
作者:hejianzhong VB.Net中文站(http://vbnetcn.126.com)
首先介紹一下注冊(cè)表的相關(guān)基礎(chǔ)知識(shí):注冊(cè)表的每一個(gè)文件夾被稱為一個(gè)Key(項(xiàng)),這個(gè)文件夾的子文件夾被稱為SubKey(子
項(xiàng)),而在一個(gè)子項(xiàng)中有不同的Value Name(值項(xiàng)—即數(shù)值頂),值項(xiàng)后面就是你要保存的數(shù)據(jù)Value Data(數(shù)據(jù))了。而在子項(xiàng)中
通常都有一個(gè)Default Value 是默認(rèn)的Value Name,相信打開過注冊(cè)表的一定看得很清楚:
好了,我們現(xiàn)在看一下如何寫入注冊(cè)表。
最易的方法就是使用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] )
————————————————————————————————————————————————
不過他只能寫在一個(gè)固定的位置,寫入HKEY_CURRENT_USER//Software//VB and VBA Program Setting.當(dāng)我們需要使
用注冊(cè)表加密時(shí),總不能寫在這樣一個(gè)大家都知道的地方吧?還有當(dāng)我們要使用注冊(cè)表實(shí)現(xiàn)一些功能時(shí)(如寫入run主鍵讓程序
啟動(dòng)時(shí)自運(yùn)行)這個(gè)更是無能為力。相信大家都看過VB6的例程,很是復(fù)雜。不過我看的一個(gè)系列文章寫得很好(在我的網(wǎng)站
---VB6知識(shí)庫中有收錄)竟有七八頁。在VB.Net中就很簡(jiǎn)單了,跟上面真有些差不多。--------(不過我可是研究了很久的,很辛
苦---最近有人把我的文章改成自已的名字,我很傷心,也請(qǐng)這些人自重,再次聲明在任何地方張貼必須經(jīng)過我的同意,并明確
標(biāo)明:“作者:hejianzhong VB.Net中文站 http:://vbnetcn.126.com.”的字樣)
我在這里寫了一個(gè)模塊,只是為了示例,大家在使用的時(shí)候記住不需要像我這樣另外使用模塊(應(yīng)該根據(jù)你的需要靈活運(yùn)
用),這有些畫蛇添足。這個(gè)我也不打算提供源碼下載,因?yàn)閷?shí)在太簡(jiǎn)單了。
首先如圖添加控件:
textbox控件的text設(shè)為“”,其它的請(qǐng)作相應(yīng)的修改,實(shí)際上這個(gè)示例你不編也可以,只要看懂下面的內(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 = "打開注冊(cè)表子項(xiàng)(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ù)值項(xiàng)").ToString End Sub Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) writeval("數(shù)值項(xiàng)", textbox2.Text) : textbox2.Text = "已寫入到注冊(cè)表" End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) If objkey = Nothing Then msgbox("請(qǐng)先在上面選定注冊(cè)表根項(xiàng)") 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 '定義一個(gè)objKey 的注冊(cè)項(xiàng)對(duì)象 Public objKey As Microsoft.Win32.RegistryKey '_________________________________________________________________________________________________________________ '使用openremotebasekey方法打開一個(gè)注冊(cè)表根項(xiàng)目,使用rgistryhive獲取一個(gè)最上層的Key 根項(xiàng)目 Public Sub OpenBaseKey(ByVal basekey As Microsoft.Win32.RegistryHive) objkey = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(basekey, SystemInformation.ComputerName) End Sub '__________________________________________________________________________________________________ '使opensubkey打開一個(gè)指定的子項(xiàng)目, 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)造一個(gè)新的subkey子項(xiàng)目 Public Sub NewSubKey(ByVal keyname As String) objkey = objKey.CreateSubKey(keyname) '在這里我們創(chuàng)造了一個(gè)新的subkey(項(xiàng)) 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 '_________ '刪除一個(gè)注冊(cè)表項(xiàng) Public Sub delKey(ByVal keyname As String) objkey.DeleteSubKey(keyname) End Sub '___________________________________________________________________________________________________________() '____________________________________________________________________________________________________________________ '刪除一個(gè)數(shù)據(jù)------ValueData Public Sub delval(ByVal valname As String) objkey.DeleteValue(valname) '刪除了一個(gè)值 objkey.Close() '記住在使用過后把他關(guān)閉,這通常是被提倡的好習(xí)慣。 End Sub End Module 大家看了是不是很簡(jiǎn)單,我曾在論壇看到人說,VB6用得很好我為什么要用VB.Net.而且Net只是Net技術(shù),我們的這個(gè)……..不相關(guān)。這 真是天大的誤解。就因?yàn)楹竺鎺Я艘粋(gè)net,大家都不理解并疏遠(yuǎn)他,不論怎么樣,VB.Net因?yàn)槊嫦驅(qū)ο罂梢愿行实膶懗绦�。大家為什么要拒絕呢? 我的系列文單出來以后,學(xué)VB.Net的人也增加了不少,我在網(wǎng)易論壇看到一個(gè)網(wǎng)友說為了裝VB.Net裝了一下午沒裝上,后來我告訴他到我的網(wǎng)站立馬搞定了。 所以我并不反對(duì)張貼我的文章(我自已在討論組宣傳VB.Net又有打廣告的嫌疑),可是你總要跟我打個(gè)招呼吧?我沒有私心,一心為大家,可你們?yōu)槭裁匆?把我的名字與網(wǎng)址隱藏起來呢?更有甚者,竟公然把文章作者換成自已的名字(在網(wǎng)易我的《VB.Net圖解入門》《一步一步安裝VB.Net》竟被改成"作者:大頭鬼",---已被管理員刪了!,)�?吹竭@些,我的心里有點(diǎn)涼。前幾天因?yàn)榫W(wǎng)速太慢,我通宵申請(qǐng)了一個(gè)高 速的網(wǎng)站,連夜上傳所有資料,誰知上傳又奇慢,兩天,在網(wǎng)上呆了十幾個(gè)小時(shí)。希望大家不要再這樣的打擊我了 ,這不是什么名利的問題,而是一個(gè)人起碼的尊嚴(yán)被杜