我正在寻找一个可以找到注册表项并将其删除的脚本。我的密钥就像:“{24EAA2C1-3EE7-40E0-AEA3-D20AA14A6005}”。此密钥存储在许多地方:
HKEY_CLASSES_ROOT\CLSID\{MY_KEY}
HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{MY_KEY}
HKEY_CURRENT_USER\Software\Classes\CLSID\{MY_KEY}
HKEY_CURRENT_USER\Software\Classes\Wow6432Node\CLSID\{MY_KEY}
HKEY_USERS\S-1-5-21-1786904987-2011555162-1551513139-1001\Software\Classes\CLSID\{MY_KEY}
HKEY_USERS\S-1-5-21-1786904987-2011555162-1551513139-1001\Software\Classes\Wow6432Node\CLSID\{KEY}
HKEY_USERS\S-1-5-21-1786904987-2011555162-1551513139-1001_Classes\CLSID\{MY_KEY}
HKEY_USERS\S-1-5-21-1786904987-2011555162-1551513139-1001_Classes\Wow6432Node\CLSID\{MY_KEY}
但是这些位置可能会有所不同(这就是为什么我必须在删除它们之前找到它们的原因)。我想使用 C# 脚本(在控制台应用程序中)删除此键的每次出现
我试过这个:
static void Main(string[] args)
{
DeleteKeyTest("{MY_KEY}");
}
private RegistryKey baseRegistryKey = Registry.ClassesRoot;
public void DeleteKeyTest(string KeyName)
{
try
{
RegistryKey rk = baseRegistryKey;
rk.DeleteSubKey(KeyName);
}
catch (Exception e)
{
}
}
我收到一个错误:
不能删除子项,因为它不存在。
编辑
这是我的代码,用于查找密钥的所有出现:
public List<string> SearchKey(string Key)
{
List<string> ListKeys = getKeyPath(Key, RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry64));
ListKeys.AddRange(getKeyPath(Key, RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64)));
ListKeys.AddRange(getKeyPath(Key, RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)));
ListKeys.AddRange(getKeyPath(Key, RegistryKey.OpenBaseKey(RegistryHive.Users, RegistryView.Registry64)));
ListKeys.AddRange(getKeyPath(Key, RegistryKey.OpenBaseKey(RegistryHive.CurrentConfig, RegistryView.Registry64)));
return ListKeys;
}
private List<string> getKeyPath(string key, RegistryKey registryKey)
{
List<string> keys = new List<string>();
if (CanReadRegistryKey(registryKey))
{
foreach (string keyname in registryKey.GetSubKeyNames())
{
if (keyname.Contains(key))
keys.Add(registryKey.Name + "\\" + keyname);
if (CanReadKey(registryKey, keyname))
keys.AddRange(getKeyPath(key, registryKey.OpenSubKey(keyname, RegistryKeyPermissionCheck.ReadSubTree)));
}
}
return keys;
}
private bool CanReadRegistryKey(RegistryKey registryKey)
{
try
{
registryKey.GetSubKeyNames();
return true;
}
catch (UnauthorizedAccessException e)
{
return false;
}
}
private bool CanReadKey(RegistryKey registryKey, string keyname)
{
try
{
registryKey.OpenSubKey(keyname, RegistryKeyPermissionCheck.ReadSubTree);
return true;
}
catch (SecurityException e)
{
return false;
}
}
用法 :
List<string> ListKeys = SearchKey("MY_KEY");
我并不总是得到相同的结果。有时我找到 7 个密钥,有时找到 8 个。(缺少的密钥是 HKEY_CLASSES_ROOT\Wow6432Node\CLSID{MY_KEY})
这是我的删除代码:
public int RemoveKey(string Key)
{
int count = RemoveKeys(Key, RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry64));
count += RemoveKeys(Key, RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64));
count += RemoveKeys(Key, RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64));
count += RemoveKeys(Key, RegistryKey.OpenBaseKey(RegistryHive.Users, RegistryView.Registry64));
count += RemoveKeys(Key, RegistryKey.OpenBaseKey(RegistryHive.CurrentConfig, RegistryView.Registry64));
return count;
}
private int RemoveKeys(string key, RegistryKey registryKey)
{
int count = 0;
if (CanReadRegistryKey(registryKey))
{
foreach (string keyname in registryKey.GetSubKeyNames())
{
count += RemoveKeys(key, registryKey.OpenSubKey(keyname));
try
{
if (keyname.Contains(key))
{
registryKey.DeleteValue(keyname);
count++;
}
}
catch (Exception e)
{
}
}
}
return count;
}
用法 :
int NumberOfRemovedKeys = RemoveKey("MY_KEY");
但我总是在这段代码上遇到错误:
registryKey.DeleteValue(keyname);
错误信息 :
System.UnauthorizedAccessException:无法写入注册表项。
当我尝试以管理员模式启动应用程序时,我遇到了同样的错误。但是当我尝试从 regedit 中手动删除密钥时,没有问题。
在“搜索”代码中,我得到了一个包含完整路径的键列表。我尝试使用完整路径直接删除密钥,但这是不可能的,因为必须使用 RegistryHive 初始化 RegistryKey(例如 RegistryHive.ClassesRoot),但 RegistryHive 已经在完整路径中。
如何获得删除授权?