在《用Visual C#讀取注冊信息》的文中,已經介紹了用 Visual C#來讀取注冊表中的注冊信息。本文就來介紹用Visual C#對注冊表的另外一個操作,這也是一個具有破壞性的操作過程--刪除注冊信息。
在上文中已經知道,由于Visual C#本身沒有帶類庫,他對注冊表的處理過程是通過調用.Net FrameWork SDK中的名稱空間Microsoft.Win32中封裝的二個類來實現的。這二個類就是Registry類、RegistryKey類。在RegistryKey類中定義了三個方法來刪除注冊表中的注冊信息。他們分別是:DeleteSubKey ( )方法、DeleteSubKeyTree ( )方法、DeleteValue ( )方法。下面就具體介紹一下在Visual C#中如何正確使用這三個方法。
一.如何用Visual C#中調用這三個方法: 在介紹如何使用這三個方法之前,還需要重新介紹一下RegistryKey類中的一個方法--OpenSubKey ( )方法。在上一文中已經介紹了,此方法是打開指定的子鍵。其實OpenSubKey( )方法有二種調用的方式:
I > .OpenSubKey ( string , subkey ) :這種調用方式是對于此子鍵只是進行讀操作。 II > .OpenSubKey ( string subkey , Boolean writable ):當對子鍵使用寫操作的時候要用此種調用方法。如果在對子鍵使用了寫操作,但仍然使用第一種調用方法,在程序運行的時候會產生一個錯誤信息。
(1). DeleteSubKey ( )方法: 此方法是刪除一個指定的子鍵,在使用此方法的時候,如果在此子鍵中還存在另外的子鍵,則會產生一個錯誤信息。在程序中調用此方法有二種原型,為: I > . DeleteSubKey ( string , subkey ):這種調用方式就是直接刪除指定的子鍵。
II > . DeleteSubKey ( string subkey , Boolean info ):其中的"string"是要刪除的子鍵的名稱,"Boolean"參數的意思是:如果值為"True",則在程序調用的時候,刪除的子鍵不存在,則產生一個錯誤信息;如果值為"False",則在程序調用的時候,刪除的子鍵不存在,也不產生錯誤信息,程序依然正確運行。所以在具體的程序設計過程中,我還是推薦使用第二種調用方法。
(2). DeleteSubKeyTree ( )方法: 此方法是徹底刪除指定的子鍵目錄,即:刪除該子鍵以及該子鍵以下的全部子鍵。由于此方法的破壞性是非常強的,所有在使用的時候要非常主要。在程序中調用此方法的原型就一種,為:
DeleteSubKeyTree ( string subkey ):其中"subkey"就是要徹底刪除的子鍵名稱。
(3). DeleteValue ( )方法: 此方法是刪除指定的鍵值。在程序中調用此方法的原型就一種,為: DeleteValue ( string value ):其中"value"就是要刪除的鍵值的名稱。 在介紹完與刪除注冊表中注冊信息有關方法后,將通過一個程序來說明他們在程序中具體用法。
二. 程序設計和運行環境以及要準備的工作: I > .視窗系統2000服務器版
II > ..Net FrameWork SDK Beta 2版
III > .由于程序的功能是刪除指定的主鍵、子鍵和鍵值,這就需要我們在注冊表中先為設置好這些值的位置和名稱。具體如下: 在HKEY_LOCAL_MACHINE主鍵下面的"SOFTWARE"子鍵中建立如下子鍵和鍵值: 在"SOFTWARE"子鍵下建立"aaa"子鍵。在"aaa"子鍵下面建立"bbb"子鍵和"ddd"子鍵。在"bbb"子鍵中建立名稱為"ccc"的鍵值,鍵值的值為"ccc"。子"ddd"子鍵中建立子鍵"eee",并在此子鍵中建立一個"fff"鍵值,鍵值的值為"fff"。程序中要刪除的鍵值是"ccc"鍵值,要刪除的子鍵是"bbb",要徹底刪除的子鍵是"ddd"。具體設定如下圖所示:
點擊小圖放大圖01:為程序設定的注冊表結構圖
三. 程序設計的重要步驟: 程序設計的主要步驟就是如何刪除鍵值、不包含任何子鍵的子鍵、包含子鍵的子鍵。下面就通過程序來具體說明: (1).如何刪除鍵值。在程序中要刪除鍵值是"ccc"。以下就是程序中刪除此鍵值的具體語句。 RegistryKey hklm = Registry.LocalMachine ; RegistryKey software = hklm.OpenSubKey ( "SOFTWARE", true ) ; //打開"SOFTWARE"子鍵 RegistryKey no1 = software.OpenSubKey ( "aaa", true ) ; //打開"aaa"子鍵 RegistryKey no2 = no1.OpenSubKey ( "bbb" , true ) ; //打開"bbb"子鍵 no2.DeleteValue( "ccc" ) ; //刪除名稱為"ccc"的鍵值
(2).如何刪除不包含任何子鍵的子鍵。在程序要刪除的子鍵是"bbb"。以下就是刪除此子鍵的具體程序代碼: RegistryKey hklm = Registry.LocalMachine ; RegistryKey software = hklm.OpenSubKey ( "SOFTWARE", true ) ; //打開"SOFTWARE"子鍵 RegistryKey no1 = software.OpenSubKey ( "aaa", true ) ; //打開"aaa"子鍵 no1.DeleteSubKey ( "bbb", false ); //刪除名稱為"bbb"的子鍵
(3).如何刪除包含子鍵的子鍵。在程序中要刪除的此子鍵是"ddd"。以下就是刪除此子鍵的具體程序代碼: RegistryKey hklm = Registry.LocalMachine ; hklm.DeleteSubKey ( "aaa", false ); RegistryKey software = hklm.OpenSubKey ( "SOFTWARE", true ) ; //打開"SOFTWARE"子鍵 RegistryKey no1 = software.OpenSubKey ( "aaa", true ) ; //打開"aaa"子鍵 no1.DeleteSubKeyTree ( "ddd" ); //刪除名稱為"ddd"的子鍵
四. 本文中的程序源代碼( reg.cs )以及運行界面: reg.cs程序的主要功能就是刪除注冊表中的鍵值、不包含子鍵的子鍵和包含子鍵的子鍵。并且通過按鈕"讀取注冊表",以列表的顯示方法來及時了解刪除的情況。下圖就是程序運行后的界面:
點擊小圖放大圖02:本文中程序的運行界面
reg.cs程序源代碼如下: using System ; using System.Drawing ; using System.Collections ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data ; using Microsoft.Win32 ; public class Form1 : Form { private System.ComponentModel.Container components ; private ListBox listBox1 ; private Button button1 ; private Button button2 ; private Button button3 ; private Button button4 ; public Form1 ( ) { InitializeComponent ( ) ; } //清除在程序中使用過的資源 public override void Dispose ( ) { base.Dispose ( ) ; components.Dispose ( ) ; } //初始化程序中使用到的組件 private void InitializeComponent ( ) { components = new System.ComponentModel.Container ( ) ; button1 = new Button ( ) ; button2 = new Button ( ) ; button3 = new Button ( ) ; button4 = new Button ( ) ; listBox1 = new ListBox ( ) ; button1.Location = new System.Drawing.Point ( 16 , 320 ) ; button1.Size = new System.Drawing.Size ( 75 , 23 ) ; button1.TabIndex = 0 ; button1.Text = "讀取注冊表" ; button1.Click += new System.EventHandler ( button1_Click ) ;
button2.Location = new System.Drawing.Point ( 116 , 320 ) ; button2.Size = new System.Drawing.Size ( 75 , 23 ) ; button2.TabIndex = 0 ; button2.Text = "刪除鍵值ccc" ; button2.Click += new System.EventHandler ( button2_Click ) ;
button3.Location = new System.Drawing.Point ( 216 , 320 ) ; button3.Size = new System.Drawing.Size ( 75 , 23 ) ; button3.TabIndex = 0 ; button3.Text = "刪除子鍵bbb" ; button3.Click += new System.EventHandler ( button3_Click ) ;
button4.Location = new System.Drawing.Point ( 316 , 320 ) ; button4.Size = new System.Drawing.Size ( 75 , 23 ) ; button4.TabIndex = 0 ; button4.Text = "刪除主鍵ddd" ; button4.Click += new System.EventHandler ( button4_Click ) ;
listBox1.Location = new System.Drawing.Point ( 16 , 32 ) ; listBox1.Size = new System.Drawing.Size ( 496 , 264 ) ; listBox1.TabIndex = 1 ;
this.Text = "用Visual C#來刪除注冊表中的主鍵、子鍵和鍵值!" ; this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ; this.ClientSize = new System.Drawing.Size ( 528 , 357 ) ; this.Controls.Add ( listBox1 ) ; this.Controls.Add ( button1 ) ; this.Controls.Add ( button2 ) ; this.Controls.Add ( button3 ) ; this.Controls.Add ( button4 ) ; } protected void button1_Click ( object sender , System.EventArgs e ) { listBox1.Items.Clear ( ) ; RegistryKey hklm = Registry.LocalMachine ; RegistryKey software = hklm.OpenSubKey ( "SOFTWARE" ) ; //打開"SOFTWARE"子鍵 RegistryKey no1 = software.OpenSubKey ( "aaa" ) ; //打開"aaa"子鍵 foreach ( string site in no1.GetSubKeyNames ( ) ) //開始遍歷由子鍵名稱組成的字符串數組 { listBox1.Items.Add ( site ) ; //在列表中加入子鍵名稱 RegistryKey sitekey = no1.OpenSubKey ( site ) ; //打開此子鍵 foreach ( string sValName in sitekey.GetValueNames ( ) ) //開始遍歷由指定子鍵擁有的鍵值名稱組成的字符串數組 { listBox1.Items.Add ( " " + sValName + ": " + sitekey.GetValue ( sValName ) ) ; //在列表中加入鍵名稱和對應的鍵值 } } } protected void button2_Click ( object sender , System.EventArgs e ) { RegistryKey hklm = Registry.LocalMachine ; RegistryKey software = hklm.OpenSubKey ( "SOFTWARE", true ) ; //打開"SOFTWARE"子鍵 RegistryKey no1 = software.OpenSubKey ( "aaa", true ) ; //打開"aaa"子鍵 RegistryKey no2 = no1.OpenSubKey ( "bbb" , true ) ; //打開"bbb"子鍵 no2.DeleteValue( "ccc" ) ; //刪除名稱為"ccc"的鍵值 } protected void button3_Click ( object sender , System.EventArgs e ) { RegistryKey hklm = Registry.LocalMachine ; RegistryKey software = hklm.OpenSubKey ( "SOFTWARE", true ) ; //打開"SOFTWARE"子鍵 RegistryKey no1 = software.OpenSubKey ( "aaa", true ) ; //打開"aaa"子鍵 no1.DeleteSubKey ( "bbb", false ); //刪除名稱為"bbb"的子鍵 } protected void button4_Click ( object sender , System.EventArgs e ) { RegistryKey hklm = Registry.LocalMachine ; hklm.DeleteSubKey ( "aaa", false ); RegistryKey software = hklm.OpenSubKey ( "SOFTWARE", true ) ; //打開"SOFTWARE"子鍵 RegistryKey no1 = software.OpenSubKey ( "aaa", true ) ; //打開"aaa"子鍵 no1.DeleteSubKeyTree ( "ddd" ); //刪除名稱為"ddd"的子鍵 } public static void Main ( ) { Application.Run ( new Form1 ( ) ) ; } }
五. 總結: 本文介紹Visual C#注冊表編程的一個重要內容,即:如何刪除注冊信息。由于刪除注冊信息是一項非常具有破壞性的操作,所以在操作之前一定要注意對注冊表的保護工作。
|