reg.cs程序源代码如下:
1using System;
2using System.Drawing;
3using System.Collections;
4using System.ComponentModel;
5using System.Windows.Forms;
6using System.Data;
7using Microsoft.Win32;
8public class Form1 : Form
9{
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#注册表编程的一个重要内容,即:如何删除注册信息。由于删除注册信息是一项非常具有破坏性的操作,所以在操作之前一定要注意对注册表的保护工作。