Visual C#來刪除註冊表中的註冊資訊

來源:互聯網
上載者:User
visual|註冊表

在《用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#註冊表編程的一個重要內容,即:如何刪除註冊資訊。由於刪除註冊資訊是一項非常具有破壞性的操作,所以在操作之前一定要注意對註冊表的保護工作。

 


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.