首页上一页 1 下一页尾页 1 条记录 1/1页
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();
//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();