您的当前位置: 源码爱好者 » 网页制作教程(编程教程) » C#技术教程

C#使用Registry和RegistryKey两个类读写注册表

  • 标签:读写注册表 更新时间:2014-05-11
  • C#读写注册表操作,主要是使用Microsoft.Win32中的Registry和RegistryKey两个类。最开始是让用户选择窗体颜色,然后将颜色信息写入注册表中。读注册表信息:读取注册表中特定键的信息,并显示在ListBox控件中,依次打开SOFTWARE和ChoseColor子键,遍历由指定子键拥有的键值名称组成的字符串数组;写注册表:RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", true);

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using Microsoft.Win32;// 用到Registry和RegistryKey两个类
    namespace 读写注册表
    {
    	public class Form1 : System.Windows.Forms.Form
    	{
    		private System.Windows.Forms.Button button1;
    		private System.Windows.Forms.Button button2;
    		private System.Windows.Forms.ColorDialog colorDialog1;
    		private System.Windows.Forms.ListBox listBox1;
    		private System.ComponentModel.Container components = null;
    		public Form1()
    		{
    			InitializeComponent();
    			if (ReadRegistry() == false)
    				listBox1.Items.Add("注册表中无相关信息!");
    		}
    		private void InitializeComponent()
    		{
    			this.button1 = new System.Windows.Forms.Button();
    			this.button2 = new System.Windows.Forms.Button();
    			this.colorDialog1 = new System.Windows.Forms.ColorDialog();
    			this.listBox1 = new System.Windows.Forms.ListBox();
    			this.SuspendLayout();
    			// button1
    			this.button1.Location = new System.Drawing.Point(48, 243);
    			this.button1.Name = "button1";
    			this.button1.Size = new System.Drawing.Size(64, 24);
    			this.button1.TabIndex = 1;
    			this.button1.Text = "选择颜色";
    			this.button1.Click += new System.EventHandler(this.button1_Click);
    			// button2
    			this.button2.Location = new System.Drawing.Point(184, 243);
    			this.button2.Name = "button2";
    			this.button2.Size = new System.Drawing.Size(64, 24);
    			this.button2.TabIndex = 2;
    			this.button2.Text = "刷新表框";
    			this.button2.Click += new System.EventHandler(this.button2_Click);
    			// listBox1
    			this.listBox1.ItemHeight = 12;
    			this.listBox1.Location = new System.Drawing.Point(16, 24);
    			this.listBox1.Name = "listBox1";
    			this.listBox1.Size = new System.Drawing.Size(256, 208);
    			this.listBox1.TabIndex = 1;
    			// Form1
    			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    			this.ClientSize = new System.Drawing.Size(288, 277);
    			this.Controls.Add(this.listBox1);
    			this.Controls.Add(this.button2);
    			this.Controls.Add(this.button1);
    			this.Name = "Form1";
    			this.Text = "Form1";
    			this.ResumeLayout(false);
    		}
    		#endregion
    		[STAThread]
    		static void Main()
    		{
    			Application.Run(new Form1());
    		}
    		private void button1_Click(object sender, System.EventArgs e)
    		{
    			if (colorDialog1.ShowDialog() == DialogResult.OK)
    			{
    				// 将窗体的颜色设置为用户选择的颜色
    				BackColor = colorDialog1.Color;
    				SaveToRegistry(); // 将颜色信息写入注册表中
    			}
    		}
    		private void button2_Click(object sender, System.EventArgs e)
    		{
    			listBox1.Items.Clear();  // 清空列表框中的所有项
    			// 读取注册表信息,并显示在列表框中
    			if (ReadRegistry() == false)
    			{
    				listBox1.Items.Add("注册表中无相关信息!");
    			}
    		}
    		// 读取注册表中特定键的信息,并显示在ListBox控件中
    		private bool ReadRegistry()
    		{
    			// 依次打开SOFTWARE和ChoseColor子键
    			RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("SOFTWARE");
    			RegistryKey choseColorKey = softwareKey.OpenSubKey("ChoseColor");
    			if (choseColorKey == null)
    				return false;
    			else
    			{
    				RegistryKey colorKey = choseColorKey.OpenSubKey("Color");
    				if (colorKey == null)
    					return false;
    				else
    				{
    					try
    					{
    						listBox1.Items.Add("成功打开键:" + colorKey.ToString());
    						// 读取注册表中Color键的各个键值的值
    						int redComponent = (int)colorKey.GetValue("Red");
    						int greenComponent = (int)colorKey.GetValue("Green");
    						int blueComponent = (int)colorKey.GetValue("Blue");
    						BackColor = Color.FromArgb(redComponent, greenComponent, blueComponent);
    						// 遍历由指定子键拥有的键值名称组成的字符串数组
    						foreach (string sValName in colorKey.GetValueNames())
    						{
    							listBox1.Items.Add(sValName + "分量值: " + colorKey.GetValue(sValName));
    						}
    						return true;
    					}
    					catch(Exception exc)
    					{
    						MessageBox.Show(exc.Message,"提示");
    						return false;
    					}
    				}
    			}
    		}
    		// 写入注册表
    		private void SaveToRegistry()
    		{
    			try
    			{
    				// 以可写的方式打开SOFTWARE子键
    				RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", true);
    				// 依次创建ChoseColor子键和Color子键
    				RegistryKey choseColorKey = softwareKey.CreateSubKey("ChoseColor");
    				RegistryKey colorKey = choseColorKey.CreateSubKey("Color");
    				// 设置Color键的SetValue方法可能引发异常
    				colorKey.SetValue("Red", (int)BackColor.R);
    				colorKey.SetValue("Green", (int)BackColor.G);
    				colorKey.SetValue("Blue", (int)BackColor.B);
    			}
    			catch(Exception exc)
    			{
    				MessageBox.Show(exc.Message,"提示");
    			}
    		}
    	}
    }

    完整C#源码:C#演示注册表的读写操作

  • 已读: 次 收藏本文关闭本文打印本文复制链接