递归搜索注册表的例子
- using System;
- using Microsoft.Win32;
- namespace CleanRegistry
- {
- class Program
- {
- static void Main(string[] args)
- {
- RegistryKey[] MyRootKeys = new RegistryKey[5];
- MyRootKeys[0] = Registry.CurrentUser;
- MyRootKeys[1] = Registry.ClassesRoot;
- MyRootKeys[2] = Registry.LocalMachine;
- MyRootKeys[3] = Registry.Users;
- MyRootKeys[4] = Registry.CurrentConfig;
- foreach (RegistryKey MyRootKey in MyRootKeys)
- {
- FindSymantec( MyRootKey );
- }
- }
- static void FindSymantec(RegistryKey MyKey)
- {
- //Console.WriteLine("Searching { 0 }...", MyKey.ToString());
- // 获取所有键名
- foreach (string MyValueName in MyKey.GetValueNames())
- {
- // 键类型
- if( MyKey.GetValueKind(MyValueName) == RegistryValueKind.String )
- {
- // 获取键值
- string MyValue = MyKey.GetValue(MyValueName).ToString().ToLower();
- if ( MyValue.Contains("symantec antivirus") )
- {
- Console.WriteLine("{ 0 }/t{ 1 }/t{ 2 }", MyKey, MyValueName, MyValue);
- }
- }
- }
- // 获取所有子键,递归
- if (MyKey.SubKeyCount != 0)
- {
- // 获取子键名
- foreach (string MySubKey in MyKey.GetSubKeyNames())
- {
- // 打开子键
- RegistryKey SubKey;
- try
- {
- SubKey = MyKey.OpenSubKey(MySubKey);
- }
- catch
- {
- continue;
- }
- // 递归
- FindSymantec(SubKey);
- }
- }
- }
- }
- }