已有57人关注
C#注册表操作大全
发表在C#图书答疑 2009-06-23
是否精华
版块置顶:
1.打开键
//using Microsoft.Win32;
RegistryKey rkLocalM = Registry.LocalMachine; //Registry.ClassesRoot, Registry.LocalMachine, Registry.Users, Registry.CurrentConfig
const string strSubKey = %%1; //@"SOFTWARE\ODBC\ODBC.INI\DBNewTest\Test"
RegistryKey rkSub = rkLocalM.OpenSubKey( strSubKey );
//rkLocalM = Registry.LocalMachine; //Registry.ClassesRoot, Registry.LocalMachine, Registry.Users, Registry.CurrentConfig
//rkLocalM.OpenSubKey(%%1);

2.添加键
//using Microsoft.Win32;
RegistryKey rkLocalM = Registry.LocalMachine; //Registry.ClassesRoot, Registry.LocalMachine, Registry.Users, Registry.CurrentConfig
const string strSubKey = %%1; //@"SOFTWARE\ODBC\ODBC.INI\DBNewTest\Test"
RegistryKey rkSub = rkLocalM.CreateSubKey( strSubKey );
rkLocalM.Close();

3.删除键
rkSub.DeleteSubKey( %%1, false );

4.枚举第一个键
//using System.Collections;
IEnumerator<string> it = SubKey.GetSubKeyNames().First();
string %%1=it.Current;

5.枚举下一个键
string %%1=null;
if(it.MoveNext());
%%1=it.Current;

6.获取DWORD值
%%2=rkSub.GetValue(%%1);

7.获取二进制值
%%2=rkSub.GetValue(%%1);

8.读取字符串值
%%2=rkSub.GetValue(%%1);

9.写入字符串值
rkSub.SetValue( %%1, "Test" );

10.写入二进制值
rkSub.SetValue(%%1, value, RegistryValueKind.Binary);

11.写入DWORD值
rkSub.SetValue(%%1, value, RegistryValueKind.DWord);

12.设置DWORD值
rkSub.Add(%%1, value, RegistryValueKind.DWord); 

13.删除值
rkSub.DeleteValue( %%1, false );//"DriverID"

14.关闭键
rkSub.Close();

15.列出一个键下所有的子键
string[] strSubKeys = rkSub.GetSubKeyNames();
for( int i = 0; i < strSubKeys.Length; i++ )
{
string %%1= strSubKeys[i] ;
%%2
}

16.列出一个键下所有的值
string[] strData = rkSub.GetValueNames();
for( int i = 0; i < strData.Length; i++ )
{
    //Debug.WriteLine( string.Format( "{0}:{1}", strData[i], rkSub.GetValue( strData[i] ) ) );
}

17.列出一个键下所有的子键和值
string[] strSubKeys = rkSub.GetSubKeyNames();
for( int i = 0; i < strSubKeys.Length; i++ )
{
    string %%1=strSubKeys[i];
%%2
}
string[] strData = rkSub.GetValueNames();
for( int i = 0; i < strData.Length; i++ )
{
    //Debug.WriteLine( string.Format( "{0}:{1}", strData[i], rkSub.GetValue( strData[i] ) ) );
}


18.枚举所有的子键和值
//using Microsoft.Win32;
        private static void Access_Registry(RegistryKey keyR, String str)
        {
            string[] subkeyNames;
            string[] subvalueNames;
            try
            {
                RegistryKey aimdir = keyR.OpenSubKey(str, true);
                subvalueNames = aimdir.GetValueNames();
                foreach (string valueName in subvalueNames)
                {
                    if (valueName.ToUpper().IndexOf(%%1) > -1) //"XMS"
                    {
                        Console.WriteLine(valueName);
                    }
                    string value = aimdir.GetValue(valueName) as string;
                    if (value != null)
                    {
                        if (value.ToUpper().IndexOf(%%1) > -1) //"XMS"
                        {
                            Console.WriteLine(value);
                        }

                    }
                }
                subkeyNames = aimdir.GetSubKeyNames();
                foreach (string keyName in subkeyNames)
                {
                    if (keyName.ToUpper().IndexOf(%%1) > -1) //"XMS"
                    {
                        Console.WriteLine(keyName);
                    }
                    Access_Registry(aimdir, keyName);
                }
            }
            catch (Exception) { }
        }

            RegistryKey[] keys ={ Registry.ClassesRoot, Registry.LocalMachine, Registry.CurrentUser, Registry.CurrentConfig };
            foreach (RegistryKey key in keys)
            {
                string[] subkeyNames = key.GetSubKeyNames();
                foreach (string keyName in subkeyNames)
                {
                    Access_Registry(key, keyName);
                }
key.Close();
            }

19.模糊搜索所有的子键和值,查找特定字符串并删除
//using System.Collections;using Microsoft.Win32;
            RegistryKey[] keys ={ Registry.LocalMachine, Registry.Users };// Registry.ClassesRoot, Registry.CurrentConfig
            foreach (RegistryKey key in keys)
            {
                string[] subkeys = key.GetSubKeyNames();
                Queue<String> al = new Queue<String>(subkeys);
                Queue<RegistryKey> qu = new Queue<RegistryKey>();
                for (int i = 0; i < subkeys.Length; i++)
                    qu.Enqueue(key);
                while (al.Count > 0)
                {
                    string[] subkeyNames;
                    string[] subvalueNames;
                    try
                    {
                        RegistryKey aimdir = qu.Dequeue();
                        aimdir = aimdir.OpenSubKey(al.Dequeue(), true);
                        subvalueNames = aimdir.GetValueNames();
                        foreach (string valueName in subvalueNames)
                        {
                            if (valueName.Equals("XMS", StringComparison.OrdinalIgnoreCase)) //%%1
                            {
                                aimdir.DeleteValue(valueName, false);
                            }
                            else
                            {
                                string value = aimdir.GetValue(valueName) as string;
                                if (value != null)
                                {
                                    if (value.Equals("XMS", StringComparison.OrdinalIgnoreCase))
                                    {
                                        aimdir.DeleteValue(value, false);
                                    }
                                }
                            }
                        }
                        subkeyNames = aimdir.GetSubKeyNames();
                        foreach (string keyName in subkeyNames)
                        {
                            if (keyName.Equals("XMS", StringComparison.OrdinalIgnoreCase))
                            {
                                aimdir.DeleteSubKey(keyName, false);
                            }
                            al.Enqueue(keyName);
                            qu.Enqueue(aimdir);
                        }
                    }
                    catch (Exception) { }
                }
                key.Close();
            }

20.清空一个子键
//using Microsoft.Win32;
RegistryKey rkLocalM = Registry.LocalMachine; //Registry.ClassesRoot, Registry.LocalMachine, Registry.Users, Registry.CurrentConfig
const string strSubKey = %%1; //@"SOFTWARE\ODBC\ODBC.INI"
RegistryKey rkSub = rkLocalM.OpenSubKey( strSubKey, true );
rkSub.DeleteSubKeyTree(%%2); //"DBNewTest"
rkLocalM.Close();
RegistryKey rkSub = rkLocalM.CreateSubKey( strSubKey +"\"+%%2);
rkLocalM.Close();
分享到:
精彩评论 1
小科_mrkj
学分:43 LV2
2009-06-24
沙发
读者朋友:
    您好,非常感谢您提出的建议。
首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照