`
gaofen100
  • 浏览: 1187382 次
文章分类
社区版块
存档分类
最新评论

C#注册表

 
阅读更多

由于Visual C#本身没有类库,是通过.Net FrameWork SDK(软件开发包)定义的一些类来实现对注册表的操作的。这就是名称空间Microsoft.Win32中封装的二个类:Registry类和RegistryKey类。在RegistryKey类中定义了二个方法用来创建注册表中的主键、子键和键值。它们是CreateSubValue ( )方法和SetValue ( )方法。那么如何用Visual C#来修改注册信息?在本文中,我们只介绍修改注册表中的键值的方法。而对于主键和子键,由于.Net FrameWork SDK中还没有定义这方面的方法,所以还无法完成安全的修改注册表中的信息。

一.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 ( )方法来实现的,以下是程序中创建子键的源程序:

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

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

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

(3)如何修改注册信息。由于注册表中的信息十分重要,所以一般不要对其进行写的操作。也可能是这个原因,在.Net FrameWork SDK 中并没有提供修改注册表键的方法。而只是提供了一个危害性相对较小的方法--SetValue ( ),通过这个方法,我们可以来修改键值。下面程序代码是修改一个指定键值名称的键值。当然由于SetValue( )方法的特性,如果它检测到这个键值不存在,就会创建一个新的键值。

listBox1.Items.Clear ( ) ;
RegistryKey hklm = Registry.LocalMachine ;
RegistryKey software = hklm.OpenSubKey ( "HARDWARE", true ) ;
RegistryKey dddw = software.OpenSubKey ( "aaa" , true ) ;
dddw.SetValue ( "bbb" , "abcd" ) ;

四.本文中源程序代码( reg.cs )以及编译后的程序运行界面:
以下是程序运行的界面:

reg.cs程序代码如下:

using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
using Microsoft.Win32 ;
//导入使用到的名称空间

public class Form1 : Form
{
private System.ComponentModel.Container components ;
private ListBox listBox1 ;
private Button button1 ;
private Button button2 ;
private Button button3 ;
private Button button4 ;

public Form1 ( )
{
InitializeComponent ( ) ;
}
//清除在程序中使用过的资源
public override void Dispose ( )
{
base.Dispose ( ) ;
components.Dispose ( ) ;
}
//初始化程序中使用到的组件
private void InitializeComponent ( )
{
this.components = new System.ComponentModel.Container ( ) ;
this.button1 = new Button ( ) ;
this.listBox1 = new ListBox ( ) ;
button1.Location = new System.Drawing.Point ( 16 , 320 ) ;
button1.Size = new System.Drawing.Size ( 90 , 23 ) ;
button1.TabIndex = 0 ;
button1.Text = "读取注册表" ;
button1.Click += new System.EventHandler ( this.button1_Click ) ;

this.button2 = new Button ( ) ;
button2.Location = new System.Drawing.Point ( 116 , 320 ) ;
button2.Size = new System.Drawing.Size ( 90 , 23 ) ;
button2.TabIndex = 1 ;
button2.Text = "创建子键" ;
button2.Click += new System.EventHandler ( this.button2_Click ) ;

this.button3 = new Button ( ) ;
button3.Location = new System.Drawing.Point ( 216 , 320 ) ;
button3.Size = new System.Drawing.Size ( 90 , 23 ) ;
button3.TabIndex = 2 ;
button3.Text = "创建主键" ;
button3.Click += new System.EventHandler ( this.button3_Click ) ;

this.button4 = new Button ( ) ;
button4.Location = new System.Drawing.Point ( 316 , 320 ) ;
button4.Size = new System.Drawing.Size ( 90 , 23 ) ;
button4.TabIndex = 3 ;
button4.Text = "重命名键值" ;
button4.Click += new System.EventHandler ( this.button4_Click ) ;

listBox1.Location = new System.Drawing.Point ( 16 , 32 ) ;
listBox1.Size = new System.Drawing.Size ( 496 , 264 ) ;
listBox1.TabIndex = 4 ;
this.Text = "用Visual C#来创建和修改注册表中的注册信息!" ;
this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;
this.ClientSize = new System.Drawing.Size ( 528 , 357 ) ;
//在窗体中加入组件
this.Controls.Add ( this.listBox1 ) ;
this.Controls.Add ( this.button1 ) ;
this.Controls.Add ( this.button2 ) ;
this.Controls.Add ( this.button3 ) ;
this.Controls.Add ( this.button4 ) ;
}
//以列表形式显示"HARDWARE"下面一层的子键和键值
protected void button1_Click ( object sender , System.EventArgs e )
{
listBox1.Items.Clear ( ) ;
RegistryKey hklm = Registry.LocalMachine ;
RegistryKey software = hklm.OpenSubKey ( "HARDWARE" ) ;
//打开"SYSTEM"子键
foreach ( string site in software.GetSubKeyNames ( ) )
//开始遍历由子键名称组成的字符串数组
{
listBox1.Items.Add ( site ) ;
//在列表中加入子键名称
RegistryKey sitekey = software.OpenSubKey ( site ) ;
//打开此子键
foreach ( string sValName in sitekey.GetValueNames ( ) )
//开始遍历由指定子键拥有的键值名称组成的字符串数组
{
listBox1.Items.Add ( " " + sValName + ": " + sitekey.GetValue ( sValName ) ) ;
//在列表中加入键名称和对应的键值
}
}
}
//创建子键和键值
protected void button2_Click ( object sender , System.EventArgs e )
{
listBox1.Items.Clear ( ) ;
RegistryKey hklm = Registry.LocalMachine ;
RegistryKey software = hklm.OpenSubKey ( "HARDWARE", true ) ;
RegistryKey ddd = software.CreateSubKey ( "ddd" ) ;
ddd.SetValue ( "www" , "1234" );
}
//创建一个主键并创建一个键值
protected void button3_Click ( object sender , System.EventArgs e )
{
listBox1.Items.Clear ( ) ;
RegistryKey hklm = Registry.LocalMachine ;
RegistryKey software = hklm.OpenSubKey ( "HARDWARE", true ) ;
RegistryKey main1 = software.CreateSubKey ( "main" ) ;
RegistryKey ddd = main1.CreateSubKey ( "sub" ) ;
ddd.SetValue ( "value" , "1234" ) ;
}
//重命名一个存在的键值
protected void button4_Click ( object sender , System.EventArgs e )
{
listBox1.Items.Clear ( ) ;
RegistryKey hklm = Registry.LocalMachine ;
RegistryKey software = hklm.OpenSubKey ( "HARDWARE", true ) ;
RegistryKey dddw = software.OpenSubKey ( "aaa" , true ) ;
dddw.SetValue ( "bbb" , "abcd" ) ;
}
public static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
}

以下从‘读’‘写’‘删除’‘判断’四个事例实现对注册表的简单操作
1.读取指定名称的注册表的值
private string GetRegistData(string name)
{
string registData;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.OpenSubKey("XXX",true);
registData = aimdir.GetValue(name).ToString();
return registData;
}
以上是读取的注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下的XXX目录中名称为name的注册表值;

2.向注册表中写数据
private void WTRegedit(string name,string tovalue)
{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey software = hklm.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.CreateSubKey("XXX");
aimdir.SetValue(name,tovalue);
}
以上是在注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下新建XXX目录并在此目录下创建名称为name值为tovalue的注册表项;

3.删除注册表中指定的注册表项
private void DeleteRegist(string name)
{
string[] aimnames;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.OpenSubKey("XXX",true);
aimnames = aimdir.GetSubKeyNames();
foreach(string aimKey in aimnames)
{
if(aimKey == name)
aimdir.DeleteSubKeyTree(name);
}
}
以上是在注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下XXX目录中删除名称为name注册表项;

4.判断指定注册表项是否存在
private bool IsRegeditExit(string name)
{
bool _exit = false;
string[] subkeyNames;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.OpenSubKey("XXX",true);
subkeyNames = aimdir.GetSubKeyNames();
foreach(string keyName in subkeyNames)
{
if(keyName ==name)
{
_exit = true;
return _exit;
}
}
return _exit;
}
以上是在注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下XXX目录中判断名称为name注册表项是否存在,这一方法在删除注册表时已经存在,在新建一注册表项时也应有相应判断;

Visual Studio .Net以前的版本,要对注册表进行修改,则需要调用系统API,而现在则不用那么麻烦,因为.Net已经把注册表相关的操作封装到一个类中,调用的时候只要只要调用此类对象相应的属性或方法即可。

<?XML:NAMESPACE PREFIX = O />

以下就注册表这个类进行说明。

首先,要引入注册类所在的nampespace,如下:

接下来就是对注册表的操作,则第一步要像以前操作的那样,需要设定注册表的位置,例如:

RegistryKey rkLocalM = Registry.LocalMachine;

而注册表各个根的具体对应如下:

HKEY_CLASSES_ROOT

ClassesRoot

HKEY_CURRENT_USER

CurrentUser

HKEY_LOCAL_MACHINE

LocalMachine

HKEY_USERS

Users

HKEY_CURRENT_CONFIG

CurrentConfig

HKEY_DYN_DATA

DynData

HKEY_PERFORMANCE_DATA

PerformanceData

然后,就用上面初始化后的对象,来操作注册表子键。

以下就举几个常用的用例。

第一, 枚举某个子键的所含子项以及子键的值;

RegistryKey rkLocalM = Registry.LocalMachine;

const string strSubKey = @"SOFTWARE/ODBC/ODBCINST.INI/Microsoft Access Driver (*.mdb)";

RegistryKey rkSub = rkLocalM.OpenSubKey( strSubKey );

// Get sub keys

string[] strSubKeys = rkSub.GetSubKeyNames();

for( int i = 0; i < strSubKeys.Length; i++ )

Debug.WriteLine( strSubKeys[i] );

// Get data name and its value

string[] strData = rkSub.GetValueNames();

for( int i = 0; i < strData.Length; i++ )

{

Debug.WriteLine( string.Format( "{0}:{1}", strData[i],

rkSub.GetValue( strData[i] ) ) );

}

rkLocalM.Close();

第二, 创建子项;

RegistryKey rkLocalM = Registry.LocalMachine;

const string strSubKey = @"SOFTWARE/ODBC/ODBCINST.INI/Microsoft Access Driver (*.mdb)";

RegistryKey rkSub = rkLocalM.OpenSubKey( strSubKey, true );

rkSub.SetValue( "Test", "Test" );//The type of value according value itself

rkLocalM.Close();

第三, 删除子项;

RegistryKey rkLocalM = Registry.LocalMachine;

const string strSubKey = @"SOFTWARE/ODBC/ODBC.INI/DBNewTest";

RegistryKey rkSub = rkLocalM.OpenSubKey( strSubKey, true );

rkSub.DeleteValue( "DriverID", false );

rkLocalM.Close();

第四, 添加某个子键的值;

RegistryKey rkLocalM = Registry.LocalMachine;

const string strSubKey = @"SOFTWARE/ODBC/ODBC.INI/DBNewTest/Test";

RegistryKey rkSub = rkLocalM.CreateSubKey( strSubKey );

rkLocalM.Close();

第五, 删除某个子键的值;

RegistryKey rkLocalM = Registry.LocalMachine;

const string strSubKey = @"SOFTWARE/ODBC/ODBC.INI/DBNewTest";

RegistryKey rkSub = rkLocalM.OpenSubKey( strSubKey, true );

rkSub.DeleteSubKey( "Test", false );

rkLocalM.Close();

第六, 删除某个子键下所有子键和子项;

RegistryKey rkLocalM = Registry.LocalMachine;

const string strSubKey = @"SOFTWARE/ODBC/ODBC.INI";

RegistryKey rkSub = rkLocalM.OpenSubKey( strSubKey, true );

rkSub.DeleteSubKeyTree( "DBNewTest");

rkLocalM.Close();

其实用到最多的是OpenSubKey,要注意的是,如果要相对子键进行操作的话,一定要加上“true”这个值,以标明对当前打开的子键能具有可写的能力。否则,默认打开的方式,是不具有可写的能力,这样对待子项进行添加、删除以及修改等操作,会出现异常。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics