注册表读取及更改示例

 首先我们来熟悉一下C#中修改注册表的方法与函数。在VC#中提供了Registry类、RegistryKey类来实现对注册表的操作。其中Registry类封装了注册表的七个基本主健:

  Registry.ClassesRoot 对应于HKEY_CLASSES_ROOT主键
  Registry.CurrentUser 对应于HKEY_CURRENT_USER主键
  Registry.LocalMachine 对应于 HKEY_LOCAL_MACHINE主键
  Registry.User 对应于 HKEY_USER主键
  Registry.CurrentConfig 对应于HEKY_CURRENT_CONFIG主键
  Registry.DynDa 对应于HKEY_DYN_DATA主键
  Registry.PerformanceData 对应于HKEY_PERFORMANCE_DATA主键

  RegistryKey类封装了对注册表的基本操作,包括读取,写入,删除。其中读取的主要函数有:

  OpenSubKey ( string name )方法主要是打开指定的子键。
  GetSubKeyNames ( )方法是获得主键下面的所有子键的名称,它的返回值是一个字符串数组。
  GetValueNames ( )方法是获得当前子键中的所有的键名称,它的返回值也是一个字符串数组。
  GetValue ( string name )方法是指定键的键值。

  写入的函数有:

  CreateSubKey(string name)方法是增加一个子键
  SetValue(string name,string value)方法是设置一个键的键值
  删除的函数:

  DeleteSubKey ( )方法:删除一个指定的子键。
  DeleteSubKeyTree ( )方法:

  此方法是彻底删除指定的子键目录,即:删除该子键以及该子键以下的全部子键。

我的源程序如下:

/*
 注册表更改示例
 王连伟
 2005年6月4日
 */

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.Win32;

namespace WindowsApplication1
{
 /// <summary>
 /// Form2 的摘要说明。
 /// </summary>
 public class Form2 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.GroupBox groupBox1;
  private System.Windows.Forms.TextBox textBox3;
  private System.Windows.Forms.TextBox textBox2;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.CheckBox checkBox3;
  private System.Windows.Forms.CheckBox checkBox2;
  private System.Windows.Forms.CheckBox checkBox1;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button2;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form2()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.groupBox1 = new System.Windows.Forms.GroupBox();
   this.button1 = new System.Windows.Forms.Button();
   this.textBox3 = new System.Windows.Forms.TextBox();
   this.textBox2 = new System.Windows.Forms.TextBox();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.checkBox3 = new System.Windows.Forms.CheckBox();
   this.checkBox2 = new System.Windows.Forms.CheckBox();
   this.checkBox1 = new System.Windows.Forms.CheckBox();
   this.button2 = new System.Windows.Forms.Button();
   this.groupBox1.SuspendLayout();
   this.SuspendLayout();
   //
   // groupBox1
   //
   this.groupBox1.Controls.Add(this.button1);
   this.groupBox1.Controls.Add(this.textBox3);
   this.groupBox1.Controls.Add(this.textBox2);
   this.groupBox1.Controls.Add(this.textBox1);
   this.groupBox1.Controls.Add(this.checkBox3);
   this.groupBox1.Controls.Add(this.checkBox2);
   this.groupBox1.Controls.Add(this.checkBox1);
   this.groupBox1.Controls.Add(this.button2);
   this.groupBox1.Location = new System.Drawing.Point(4, 4);
   this.groupBox1.Name = "groupBox1";
   this.groupBox1.Size = new System.Drawing.Size(416, 108);
   this.groupBox1.TabIndex = 4;
   this.groupBox1.TabStop = false;
   this.groupBox1.Text = "更改设置";
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(324, 28);
   this.button1.Name = "button1";
   this.button1.TabIndex = 6;
   this.button1.Text = "确定(&O)";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // textBox3
   //
   this.textBox3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.textBox3.Enabled = false;
   this.textBox3.Location = new System.Drawing.Point(132, 76);
   this.textBox3.Name = "textBox3";
   this.textBox3.Size = new System.Drawing.Size(184, 21);
   this.textBox3.TabIndex = 5;
   this.textBox3.Text = "";
   //
   // textBox2
   //
   this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.textBox2.Enabled = false;
   this.textBox2.Location = new System.Drawing.Point(132, 52);
   this.textBox2.Name = "textBox2";
   this.textBox2.Size = new System.Drawing.Size(184, 21);
   this.textBox2.TabIndex = 4;
   this.textBox2.Text = "";
   //
   // textBox1
   //
   this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.textBox1.Enabled = false;
   this.textBox1.Location = new System.Drawing.Point(132, 24);
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(184, 21);
   this.textBox1.TabIndex = 3;
   this.textBox1.Text = "";
   //
   // checkBox3
   //
   this.checkBox3.Location = new System.Drawing.Point(4, 76);
   this.checkBox3.Name = "checkBox3";
   this.checkBox3.Size = new System.Drawing.Size(124, 20);
   this.checkBox3.TabIndex = 2;
   this.checkBox3.Text = "更改计算机名称:";
   this.checkBox3.CheckedChanged += new System.EventHandler(this.checkBox3_CheckedChanged);
   //
   // checkBox2
   //
   this.checkBox2.Location = new System.Drawing.Point(4, 48);
   this.checkBox2.Name = "checkBox2";
   this.checkBox2.Size = new System.Drawing.Size(124, 20);
   this.checkBox2.TabIndex = 1;
   this.checkBox2.Text = "更改注册组织名:";
   this.checkBox2.CheckedChanged += new System.EventHandler(this.checkBox2_CheckedChanged);
   //
   // checkBox1
   //
   this.checkBox1.Location = new System.Drawing.Point(4, 24);
   this.checkBox1.Name = "checkBox1";
   this.checkBox1.Size = new System.Drawing.Size(124, 20);
   this.checkBox1.TabIndex = 0;
   this.checkBox1.Text = "更改注册用户名:";
   this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(324, 64);
   this.button2.Name = "button2";
   this.button2.TabIndex = 6;
   this.button2.Text = "取消(&C)";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // Form2
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(424, 118);
   this.Controls.Add(this.groupBox1);
   this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
   this.MaximizeBox = false;
   this.MinimizeBox = false;
   this.Name = "Form2";
   this.ShowInTaskbar = false;
   this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
   this.Text = "注册表更改示例";
   this.Load += new System.EventHandler(this.Form2_Load);
   this.groupBox1.ResumeLayout(false);
   this.ResumeLayout(false);

  }
  #endregion

  private void button1_Click(object sender, System.EventArgs e)
  {
   RegistryKey rsk1,rsk2;
   rsk1=Registry.LocalMachine.OpenSubKey (@"SOFTWARE/Microsoft/Windows NT/CurrentVersion",true);
   rsk2=Registry.LocalMachine.OpenSubKey(@"SYSTEM/CurrentControlSet/Services/Tcpip/Parameters",true);
   if(rsk1==null && rsk2==null)
   {
    this.Text="查询错误";
   }
   else
   {
    switch(checkBox1.Checked)
    {
     case true:
      rsk1.SetValue("RegisteredOwner",textBox1.Text);
      break;
     case false:
      break;
    }

    switch(checkBox2.Checked)
    {
     case true:
      rsk1.SetValue("RegisteredOrganization",textBox2.Text);
      break;
     case false:
      break;
    }

    switch(checkBox3.Checked)
    {
     case true:
      rsk2.SetValue("Hostname",textBox3.Text);
      break;
     case false:
      break;
    }

   }
   MessageBox.Show("设置成功");;
   rsk1.Close();
   rsk2.Close();
  }

  private void button2_Click(object sender, System.EventArgs e)
  {
   this.Close();
  }

  private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
  {
   if(checkBox1.Checked==true)
   {
    textBox1.Enabled=true;
   }
   else
   {
    textBox1.Enabled=false;
   }
  }

  private void checkBox2_CheckedChanged(object sender, System.EventArgs e)
  {
   if(checkBox2.Checked==true)
   {
    textBox2.Enabled=true;
   }
   else
   {
    textBox2.Enabled=false;
   }

  }

  private void checkBox3_CheckedChanged(object sender, System.EventArgs e)
  {
   if(checkBox3.Checked==true)
   {
    textBox3.Enabled=true;
   }
   else
   {
    textBox3.Enabled=false;
   }

  }

  private void Form2_Load(object sender, System.EventArgs e)
  {
   RegistryKey rsk3,rsk4;
   rsk3=Registry.LocalMachine.OpenSubKey (@"SOFTWARE/Microsoft/Windows NT/CurrentVersion",true);
   rsk4=Registry.LocalMachine.OpenSubKey(@"SYSTEM/CurrentControlSet/Services/Tcpip/Parameters",true);
   if(rsk3==null && rsk4==null)
   {
    this.Text="Error";
   }
   else
   {
    textBox1.Text=rsk3.GetValue("RegisteredOwner").ToString();
    textBox2.Text=rsk3.GetValue("RegisteredOrganization").ToString();
    textBox3.Text=rsk4.GetValue("Hostname").ToString();
   }
   rsk3.Close();
   rsk4.Close();
  }
 }
}
测试环境:windows xp+Microsoft Visual Studio .NET 2003

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值