如何在C#中轻松操作注册表

来源:互联网 发布:淘宝助理捷易通 编辑:程序博客网 时间:2024/05/16 11:02

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

 

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

首先,要引入注册类所在的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”这个值,以标明对当前打开的子键能具有可写的能力。否则,默认打开的方式,是不具有可写的能力,这样对待子项进行添加、删除以及修改等操作,会出现异常。

 

原创粉丝点击