用Visual C# 来删除注册表中的注册信息(1)

在《用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#注册表编程的一个重要内容,即:如何删除注册信息。由于删除注册信息是一项非常具有破坏性的操作,所以在操作之前一定要注意对注册表的保护工作。

Contributors: FHL