`
isiqi
  • 浏览: 16073126 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

如何用Visual C#来创建、修改注册信息

阅读更多
在《如何读取注册信息》和《用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 ( )方法来实现的,以下是程序中创建子键的源程序:

1listBox1.Items.Clear ( ) ;
2RegistryKey hklm =Registry.LocalMachine ;
3RegistryKey software =hklm.OpenSubKey ( "HARDWARE", true) ;
4RegistryKey main1 =software.CreateSubKey ( "main") ;
5RegistryKey ddd =main1.CreateSubKey ( "sub") ;
6ddd.SetValue ( "value", "1234");


(2).如何创建一个主键,创建一个主键和创建一个子键的过程大致是差不多的。由于主键包含若干子键,所以在创建主键的时候必须注意他们的层次关系。下面这一段程序,在参考的时候,请注意一下main键和sub键之间的关系。

1listBox1.Items.Clear ( ) ;
2RegistryKey hklm =Registry.LocalMachine ;
3RegistryKey software =hklm.OpenSubKey ( "HARDWARE", true) ;
4RegistryKey main1 =software.CreateSubKey ( "main") ;
5RegistryKey ddd =main1.CreateSubKey ( "sub") ;
6ddd.SetValue ( "value", "1234") ;
7
(3).如何修改注册信息。由于注册表中的信息十分重要,所以一般不要对其进行写的操作。也可能是这个原因,在.Net FrameWork SDK 中并没有提供修改注册表键的方法。而只是提供了一个危害性相对较小的方法--SetValue ( ),通过这个方法,我们可以来修改键值。下面程序代码是修改一个指定键值名称的键值。当然由于SetValue( )方法的特性,如果它检测到这个键值不存在,就会创建一个新的键值。
1listBox1.Items.Clear ( ) ;
2RegistryKey hklm =Registry.LocalMachine ;
3RegistryKey software =hklm.OpenSubKey ( "HARDWARE", true) ;
4RegistryKey dddw =software.OpenSubKey ( "aaa", true) ;
5dddw.SetValue ( "bbb", "abcd") ;
四.本文中源程序代码( reg.cs )以及编译后的程序运行界面:

reg.cs程序代码如下:

1usingSystem;
2usingSystem.Drawing;
3usingSystem.Collections;
4usingSystem.ComponentModel;
5usingSystem.Windows.Forms;
6usingSystem.Data;
7usingMicrosoft.Win32;
8//导入使用到的名称空间
9
10publicclassForm1 : Form
11{
12privateSystem.ComponentModel.Container components;
13privateListBox listBox1;
14privateButton button1;
15privateButton button2;
16privateButton button3;
17privateButton button4;
18
19publicForm1()
20{
21InitializeComponent();
22}

23//清除在程序中使用过的资源
24publicoverridevoidDispose()
25{
26base.Dispose();
27components.Dispose();
28}

29//初始化程序中使用到的组件
30privatevoidInitializeComponent()
31{
32this.components =newSystem.ComponentModel.Container();
33this.button1 =newButton();
34this.listBox1 =newListBox();
35button1.Location =newSystem.Drawing.Point(16, 320);
36button1.Size =newSystem.Drawing.Size(90, 23);
37button1.TabIndex =0;
38button1.Text ="读取注册表";
39button1.Click +=newSystem.EventHandler(this.button1_Click);
40
41this.button2 =newButton();
42button2.Location =newSystem.Drawing.Point(116, 320);
43button2.Size =newSystem.Drawing.Size(90, 23);
44button2.TabIndex =1;
45button2.Text ="创建子键";
46button2.Click +=newSystem.EventHandler(this.button2_Click);
47
48this.button3 =newButton();
49button3.Location =newSystem.Drawing.Point(216, 320);
50button3.Size =newSystem.Drawing.Size(90, 23);
51button3.TabIndex =2;
52button3.Text ="创建主键";
53button3.Click +=newSystem.EventHandler(this.button3_Click);
54
55this.button4 =newButton();
56button4.Location =newSystem.Drawing.Point(316, 320);
57button4.Size =newSystem.Drawing.Size(90, 23);
58button4.TabIndex =3;
59button4.Text ="重命名键值";
60button4.Click +=newSystem.EventHandler(this.button4_Click);
61
62listBox1.Location =newSystem.Drawing.Point(16, 32);
63listBox1.Size =newSystem.Drawing.Size(496, 264);
64listBox1.TabIndex =4;
65this.Text ="用Visual C#来创建和修改注册表中的注册信息!";
66this.AutoScaleBaseSize =newSystem.Drawing.Size(5, 13);
67this.ClientSize =newSystem.Drawing.Size(528, 357);
68//在窗体中加入组件
69this.Controls.Add(this.listBox1);
70this.Controls.Add(this.button1);
71this.Controls.Add(this.button2);
72this.Controls.Add(this.button3);
73this.Controls.Add(this.button4);
74}

75//以列表形式显示"HARDWARE"下面一层的子键和键值
76protectedvoidbutton1_Click(objectsender, System.EventArgs e)
77{
78listBox1.Items.Clear();
79RegistryKey hklm =Registry.LocalMachine;
80RegistryKey software =hklm.OpenSubKey("HARDWARE");
81//打开"SYSTEM"子键
82foreach(stringsite insoftware.GetSubKeyNames())
83//开始遍历由子键名称组成的字符串数组
84{
85listBox1.Items.Add(site);
86//在列表中加入子键名称
87RegistryKey sitekey =software.OpenSubKey(site);
88//打开此子键
89foreach(stringsValName insitekey.GetValueNames())
90//开始遍历由指定子键拥有的键值名称组成的字符串数组
91{
92listBox1.Items.Add(""+sValName +": "+sitekey.GetValue(sValName));
93//在列表中加入键名称和对应的键值
94}

95}

96}

97//创建子键和键值
98protectedvoidbutton2_Click(objectsender, System.EventArgs e)
99{
100listBox1.Items.Clear();
101RegistryKey hklm =Registry.LocalMachine;
102RegistryKey software =hklm.OpenSubKey("HARDWARE", true);
103RegistryKey ddd =software.CreateSubKey("ddd");
104ddd.SetValue("www", "1234");
105}

106//创建一个主键并创建一个键值
107protectedvoidbutton3_Click(objectsender, System.EventArgs e)
108{
109listBox1.Items.Clear();
110RegistryKey hklm =Registry.LocalMachine;
111RegistryKey software =hklm.OpenSubKey("HARDWARE", true);
112RegistryKey main1 =software.CreateSubKey("main");
113RegistryKey ddd =main1.CreateSubKey("sub");
114ddd.SetValue("value", "1234");
115}

116//重命名一个存在的键值
117protectedvoidbutton4_Click(objectsender, System.EventArgs e)
118{
119listBox1.Items.Clear();
120RegistryKey hklm =Registry.LocalMachine;
121RegistryKey software =hklm.OpenSubKey("HARDWARE", true);
122RegistryKey dddw =software.OpenSubKey("aaa", true);
123dddw.SetValue("bbb", "abcd");
124}

125publicstaticvoidMain()
126{
127Application.Run(newForm1());
128}

129}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics