在《用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 ;2
RegistryKey software
=
hklm.OpenSubKey (
"
SOFTWARE
"
,
true
) ;3
//
打开"SOFTWARE"子键
4
RegistryKey no1
=
software.OpenSubKey (
"
aaa
"
,
true
) ;5
//
打开"aaa"子键
6
RegistryKey no2
=
no1.OpenSubKey (
"
bbb
"
,
true
) ;7
//
打开"bbb"子键
8
no2.DeleteValue(
"
ccc
"
) ;9
//
删除名称为"ccc"的键值
(2).如何删除不包含任何子键的子键。在程序要删除的子键是"bbb"。以下就是删除此子键的具体程序代码:
RegistryKey hklm
=
Registry.LocalMachine ;2
RegistryKey software
=
hklm.OpenSubKey (
"
SOFTWARE
"
,
true
) ;3
//
打开"SOFTWARE"子键
4
RegistryKey no1
=
software.OpenSubKey (
"
aaa
"
,
true
) ;5
//
打开"aaa"子键
6
no1.DeleteSubKey (
"
bbb
"
,
false
);7
//
删除名称为"bbb"的子键
(3).如何删除包含子键的子键。在程序中要删除的此子键是"ddd"。以下就是删除此子键的具体程序代码:
RegistryKey hklm
=
Registry.LocalMachine ;2
hklm.DeleteSubKey (
"
aaa
"
,
false
);3
RegistryKey software
=
hklm.OpenSubKey (
"
SOFTWARE
"
,
true
) ;4
//
打开"SOFTWARE"子键
5
RegistryKey no1
=
software.OpenSubKey (
"
aaa
"
,
true
) ;6
//
打开"aaa"子键
7
no1.DeleteSubKeyTree (
"
ddd
"
);8
//
删除名称为"ddd"的子键
四. 本文中的程序源代码( reg.cs )以及运行界面:
reg.cs程序的主要功能就是删除注册表中的键值、不包含子键的子键和包含子键的子键。并且通过按钮"读取注册表",以列表的显示方法来及时了解删除的情况。
reg.cs程序源代码如下:
using
System;2
using
System.Drawing;3
using
System.Collections;4
using
System.ComponentModel;5
using
System.Windows.Forms;6
using
System.Data;7
using
Microsoft.Win32;8
public
class
Form1 : Form9

{10
private System.ComponentModel.Container components;11
private ListBox listBox1;12
private Button button1;13
private Button button2;14
private Button button3;15
private Button button4;16
public Form1()17

{18
InitializeComponent();19
}20
//清除在程序中使用过的资源21
public override void Dispose()22

{23
base.Dispose();24
components.Dispose();25
}26
//初始化程序中使用到的组件27
private void InitializeComponent()28

{29
components = new System.ComponentModel.Container();30
button1 = new Button();31
button2 = new Button();32
button3 = new Button();33
button4 = new Button();34
listBox1 = new ListBox();35

36
button1.Location = new System.Drawing.Point(16, 320);37
button1.Size = new System.Drawing.Size(75, 23);38
button1.TabIndex = 0;39
button1.Text = "读取注册表";40
button1.Click += new System.EventHandler(button1_Click);41

42
button2.Location = new System.Drawing.Point(116, 320);43
button2.Size = new System.Drawing.Size(75, 23);44
button2.TabIndex = 0;45
button2.Text = "删除键值ccc";46
button2.Click += new System.EventHandler(button2_Click);47

48
button3.Location = new System.Drawing.Point(216, 320);49
button3.Size = new System.Drawing.Size(75, 23);50
button3.TabIndex = 0;51
button3.Text = "删除子键bbb";52
button3.Click += new System.EventHandler(button3_Click);53

54
button4.Location = new System.Drawing.Point(316, 320);55
button4.Size = new System.Drawing.Size(75, 23);56
button4.TabIndex = 0;57
button4.Text = "删除主键ddd";58
button4.Click += new System.EventHandler(button4_Click);59

60
listBox1.Location = new System.Drawing.Point(16, 32);61
listBox1.Size = new System.Drawing.Size(496, 264);62
listBox1.TabIndex = 1;63

64
this.Text = "用Visual C#来删除注册表中的主键、子键和键值!";65
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);66
this.ClientSize = new System.Drawing.Size(528, 357);67
this.Controls.Add(listBox1);68
this.Controls.Add(button1);69
this.Controls.Add(button2);70
this.Controls.Add(button3);71
this.Controls.Add(button4);72
}73
protected void button1_Click(object sender, System.EventArgs e)74

{75
listBox1.Items.Clear();76
RegistryKey hklm = Registry.LocalMachine;77
RegistryKey software = hklm.OpenSubKey("SOFTWARE");78
//打开"SOFTWARE"子键79
RegistryKey no1 = software.OpenSubKey("aaa");80
//打开"aaa"子键81
foreach (string site in no1.GetSubKeyNames())82
//开始遍历由子键名称组成的字符串数组83

{84
listBox1.Items.Add(site);85
//在列表中加入子键名称86
RegistryKey sitekey = no1.OpenSubKey(site);87
//打开此子键88
foreach (string sValName in sitekey.GetValueNames())89
//开始遍历由指定子键拥有的键值名称组成的字符串数组90

{91
listBox1.Items.Add(" " + sValName + ": " + sitekey.GetValue(sValName));92
//在列表中加入键名称和对应的键值93
}94
}95
}96
protected void button2_Click(object sender, System.EventArgs e)97

{98
RegistryKey hklm = Registry.LocalMachine;99
RegistryKey software = hklm.OpenSubKey("SOFTWARE", true);100
//打开"SOFTWARE"子键101
RegistryKey no1 = software.OpenSubKey("aaa", true);102
//打开"aaa"子键103
RegistryKey no2 = no1.OpenSubKey("bbb", true);104
//打开"bbb"子键105
no2.DeleteValue("ccc");106
//删除名称为"ccc"的键值107
}108
protected void button3_Click(object sender, System.EventArgs e)109

{110
RegistryKey hklm = Registry.LocalMachine;111
RegistryKey software = hklm.OpenSubKey("SOFTWARE", true);112
//打开"SOFTWARE"子键113
RegistryKey no1 = software.OpenSubKey("aaa", true);114
//打开"aaa"子键115
no1.DeleteSubKey("bbb", false);116
//删除名称为"bbb"的子键117
}118
protected void button4_Click(object sender, System.EventArgs e)119

{120
RegistryKey hklm = Registry.LocalMachine;121
hklm.DeleteSubKey("aaa", false);122
RegistryKey software = hklm.OpenSubKey("SOFTWARE", true);123
//打开"SOFTWARE"子键124
RegistryKey no1 = software.OpenSubKey("aaa", true);125
//打开"aaa"子键126
no1.DeleteSubKeyTree("ddd");127
//删除名称为"ddd"的子键128
}129
public static void Main()130

{131
Application.Run(new Form1());132
}133
}
五. 总结:
本文介绍Visual C#注册表编程的一个重要内容,即:如何删除注册信息。由于删除注册信息是一项非常具有破坏性的操作,所以在操作之前一定要注意对注册表的保护工作。
本文介绍如何使用Visual C#删除注册表中的键值、子键等信息,并提供了一个示例程序来展示如何正确地使用Registry类和RegistryKey类的方法。
412

被折叠的 条评论
为什么被折叠?



