在《如何讀取注冊信息》和《用Visual C#來刪除注冊表中的注冊信息》文章中,已經探討了用Visual C#來讀取、刪除注冊表中的注冊信息,在本篇文章中我們就來介紹Visual C#注冊表編程的另外二個重要的操作:創建注冊信息和修改注冊信息。
在上二篇文章中,我們已經知道,由於Visual C#本身沒有類庫,他是通過.Net框架中的.Net FrameWork SDK(軟件開發包)定義的一些類來實現對注冊表的操作。這就是名稱空間Microsoft.Win32中封裝的二個類:Registry類、RegistryKey類。在RegistryKey類中定義了二個方法用來創建注冊表中的主鍵、子鍵和鍵值。他們是CreateSubValue ( )方法和SetValue ( )方法。那麼如何用Visual C#來修改注冊信息,在本文中,我們只是介紹了修改注冊表中的鍵值的方法。而對於主鍵和子鍵,由於.Net FrameWork SDK中還沒有定義這方面的方法,所以還無法完成安全的修改注冊表中的信息。下面就先介紹如何用Visual C#來創建注冊信息。
一.Visual C#創建和修改注冊信息要調用的二個方法:
(1).CreateSubKey ( String key )方法:此方法是創建以後面的字符串為名稱的子鍵。當然這種方法不僅能夠創建子鍵,在下面介紹的程序中,也通過此種方法來創建一個主鍵。
(2).SetValue ( String name , String keyvalue )方法:此方法的作用有二點,一種可以用來重命名鍵值的數值,一種可以用來創建新的鍵值。具體情況如下:當打開的子鍵中,如果存在此鍵值,就把新值賦給他,實現重命名操作。如果不存在,則創建一個新的鍵值。
二.程序設計和運行環境以及要准備的工作:
I>視窗系統2000服務器版
II>.Net FrameWork SDK Beta 2版
III>由於在程序中,要修改一個已經存在的鍵值,所以就要預先設置好鍵值所在的位置。打開注冊表的編輯器,在"HKEY_LOCAL_MacHINE"主鍵下面的"HARDWARE"子鍵下面創建"aaa"子鍵並在此子鍵下面創建一個名稱為"bbb"的鍵值。具體如下圖所示:
三.程序的主要功能以及設計的重要步驟:
在下面介紹的程序中,主要的功能是用Visual C#在注冊表中創建一個主鍵、一個子鍵和修改一個指定的鍵值。其中要創建的子鍵的結構層次是在主鍵"HKEY_LOCAL_MacHIN"下面的"HAREWARE"主鍵下,名稱為"ddd",其中包含一個鍵值,名稱為"www",鍵值的值為"1234"。
其中的要創建的主鍵的結構層次也是在主鍵"HKEY_LOCAL_MacHIN"下面的"HAREWARE"主鍵下,名稱為"main",在此主鍵下面包含一個名稱為"sub"的子鍵和名稱為"value"鍵值,鍵值的值為"1234"。下面就來著重介紹Visual C#是如何創建和修改這些主鍵、子鍵和鍵值的。
(1).如何創建一個子鍵,在程序中是結合CreateSubKey ( )方法和SetValue ( )方法來實現的,以下是程序中創建子鍵的源程序:
1

listBox1.Items.Clear ( ) ;
2

RegistryKey hklm = Registry.LocalMachine ;
3

RegistryKey software = hklm.OpenSubKey ( "HARDWARE" , true ) ;
4

RegistryKey main1 = software.CreateSubKey ( "main" ) ;
5

RegistryKey ddd = main1.CreateSubKey ( "sub" ) ;
6

ddd.SetValue ( "value" , "1234" );
(2).如何創建一個主鍵,創建一個主鍵和創建一個子鍵的過程大致是差不多的。由於主鍵包含若干子鍵,所以在創建主鍵的時候必須注意他們的層次關系。下面這一段程序,在參考的時候,請注意一下main鍵和sub鍵之間的關系。
1

listBox1.Items.Clear ( ) ;
2

RegistryKey hklm = Registry.LocalMachine ;
3

RegistryKey software = hklm.OpenSubKey ( "HARDWARE", true ) ;
4

RegistryKey main1 = software.CreateSubKey ( "main" ) ;
5

RegistryKey ddd = main1.CreateSubKey ( "sub" ) ;
6

ddd.SetValue ( "value" , "1234" ) ;
7

(3).如何修改注冊信息。由於注冊表中的信息十分重要,所以一般不要對其進行寫的操作。也可能是這個原因,在.Net FrameWork SDK 中並沒有提供修改注冊表鍵的方法。而只是提供了一個危害性相對較小的方法--SetValue ( ),通過這個方法,我們可以來修改鍵值。下面程序代碼是修改一個指定鍵值名稱的鍵值。當然由於SetValue( )方法的特性,如果它檢測到這個鍵值不存在,就會創建一個新的鍵值。
1

listBox1.Items.Clear ( ) ;
2

RegistryKey hklm = Registry.LocalMachine ;
3

RegistryKey software = hklm.OpenSubKey ( "HARDWARE", true ) ;
4

RegistryKey dddw = software.OpenSubKey ( "aaa" , true ) ;
5

dddw.SetValue ( "bbb" , "abcd" ) ;
四.本文中源程序代碼( reg.cs )以及編譯後的程序運行界面:
reg.cs程序代碼如下:
1
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

//導入使用到的名稱空間
9

10

public class Form1 : Form
11



{
12

private System.ComponentModel.Container components;
13

private ListBox listBox1;
14

private Button button1;
15

private Button button2;
16

private Button button3;
17

private Button button4;
18

19

public Form1()
20


{
21

InitializeComponent();
22

}
23

//清除在程序中使用過的資源
24

public override void Dispose()
25


{
26

base.Dispose();
27

components.Dispose();
28

}
29

//初始化程序中使用到的組件
30

private void InitializeComponent()
31


{
32

this.components = new System.ComponentModel.Container();
33

this.button1 = new Button();
34

this.listBox1 = new ListBox();
35

button1.Location = new System.Drawing.Point(16, 320);
36

button1.Size = new System.Drawing.Size(90, 23);
37

button1.TabIndex = 0;
38

button1.Text = "讀取注冊表";
39

button1.Click += new System.EventHandler(this.button1_Click);
40

41

this.button2 = new Button();
42

button2.Location = new System.Drawing.Point(116, 320);
43

button2.Size = new System.Drawing.Size(90, 23);
44

button2.TabIndex = 1;
45

button2.Text = "創建子鍵";
46

button2.Click += new System.EventHandler(this.button2_Click);
47

48

this.button3 = new Button();
49

button3.Location = new System.Drawing.Point(216, 320);
50

button3.Size = new System.Drawing.Size(90, 23);
51

button3.TabIndex = 2;
52

button3.Text = "創建主鍵";
53

button3.Click += new System.EventHandler(this.button3_Click);
54

55

this.button4 = new Button();
56

button4.Location = new System.Drawing.Point(316, 320);
57

button4.Size = new System.Drawing.Size(90, 23);
58

button4.TabIndex = 3;
59

button4.Text = "重命名鍵值";
60

button4.Click += new System.EventHandler(this.button4_Click);
61

62

listBox1.Location = new System.Drawing.Point(16, 32);
63

listBox1.Size = new System.Drawing.Size(496, 264);
64

listBox1.TabIndex = 4;
65

this.Text = "用Visual C#來創建和修改注冊表中的注冊信息!";
66

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
67

this.ClIEntSize = new System.Drawing.Size(528, 357);
68

//在窗體中加入組件
69

this.Controls.Add(this.listBox1);
70

this.Controls.Add(this.button1);
71

this.Controls.Add(this.button2);
72

this.Controls.Add(this.button3);
73

this.Controls.Add(this.button4);
74

}
75

//以列表形式顯示"HARDWARE"下面一層的子鍵和鍵值
76

protected void button1_Click(object sender, System.EventArgs e)
77


{
78

listBox1.Items.Clear();
79

RegistryKey hklm = Registry.LocalMachine;
80

RegistryKey software = hklm.OpenSubKey("HARDWARE");
81

//打開"SYSTEM"子鍵
82

foreach (string site in software.GetSubKeyNames())
83

//開始遍歷由子鍵名稱組成的字符串數組
84


{
85

listBox1.Items.Add(site);
86

//在列表中加入子鍵名稱
87

RegistryKey sitekey = software.OpenSubKey(site);
88

//打開此子鍵
89

foreach (string sValName in sitekey.GetValueNames())
90

//開始遍歷由指定子鍵擁有的鍵值名稱組成的字符串數組
91


{
92

listBox1.Items.Add(" " + sValName + ": " + sitekey.GetValue(sValName));
93

//在列表中加入鍵名稱和對應的鍵值
94

}
95

}
96

}
97

//創建子鍵和鍵值
98

protected void button2_Click(object sender, System.EventArgs e)
99


{
100

listBox1.Items.Clear();
101

RegistryKey hklm = Registry.LocalMachine;
102

RegistryKey software = hklm.OpenSubKey("HARDWARE", true);
103

RegistryKey ddd = software.CreateSubKey("ddd");
104

ddd.SetValue("www", "1234");
105

}
106

//創建一個主鍵並創建一個鍵值
107

protected void button3_Click(object sender, System.EventArgs e)
108


{
109

listBox1.Items.Clear();
110

RegistryKey hklm = Registry.LocalMachine;
111

RegistryKey software = hklm.OpenSubKey("HARDWARE", true);
112

RegistryKey main1 = software.CreateSubKey("main");
113

RegistryKey ddd = main1.CreateSubKey("sub");
114

ddd.SetValue("value", "1234");
115

}
116

//重命名一個存在的鍵值
117

protected void button4_Click(object sender, System.EventArgs e)
118


{
119

listBox1.Items.Clear();
120

RegistryKey hklm = Registry.LocalMachine;
121

RegistryKey software = hklm.OpenSubKey("HARDWARE", true);
122

RegistryKey dddw = software.OpenSubKey("aaa", true);
123

dddw.SetValue("bbb", "abcd");
124

}
125

public static void Main()
126


{
127

Application.Run(new Form1());
128

}
129

}