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

添加人:maliping840827零级(193分)   添加时间:2007-09-07    阅读次数:1726  收藏此教程
  1using System;
  2using System.Drawing;
  3using System.Collections;
  4using System.ComponentModel;
  5using System.Windows.Forms;
  6using System.Data;
  7using Microsoft.Win32;
  8//导入使用到的名称空间
  9
 10public 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}
2页 第2上一页12下一页
相关的教程: VS2005 CSharp 注册表
收藏此教程

当前平均分: 0.0(0 次打分)

-5-4-3-2-1012345
评论主题
您的大名
您的评论
验证码 点击换一个验证码
知识库搜索: