我无法删除注册表子项

时间:2018-07-12 11:17:45

标签: c# windows registry

如果您被服务器禁止,我正在为游戏制作应用程序。您在HKEY_CURRENT_USERHKEY_CURRENT_USER\Software\Windows\

中有2个随机数字键

button1(删除HKEY_CURRENT_USER处的第一个子键)

但是当我在button2中输入相同的代码时,它在Software \ windows中的find子项却在删除时给出了错误。

string[] dummy = new string[10];

foreach (string s in Registry.CurrentUser.OpenSubKey(@"Software\\Microsoft").GetSubKeyNames())
{
    RegistryKey rk = Registry.CurrentUser.OpenSubKey(@"Software\\Microsoft" + "\\" + s);
    string Key = rk.ToString();
    string Key2 = Key.Replace("HKEY_CURRENT_USER\\Software\\Microsoft\\", "");
    bool Check = IsDigitsOnly(Key2);
    if (Check)
    {
        RegistryKey ecks = Registry.CurrentUser.OpenSubKey(@"Software\\Microsoft" + "\\" + s);
        {
            string ecks2 = ecks.ToString();
            string ecksxd = ecks2.Replace("HKEY_CURRENT_USER\\Software\\Microsoft\\", "");
            string[] hello = GetRegistry2(ecksxd);
            Registry.CurrentUser.DeleteSubKey(Key2);
        }
    }
}

项目来源

CLICK HERE TO SEE SOURCE

2 个答案:

答案 0 :(得分:0)

让我们假设您要删除HKCU /软件/ Microsoft / Blah / CurrentVersion / MyCoolSubKey中的密钥。当你做

string Key2 = Key.Replace("HKEY_CURRENT_USER\\Software\\Microsoft\\", "");

您的Key2(为什么要大写?)变为

Blah/CurrentVersion/MyCoolSubKey

然后致电

Registry.CurrentUser.DeleteSubKey(Key2);

Registry.CurrentUser.DeleteSubKey不知道Key2的来源。它只是按照您所说的去做:“从HKCU删除名为 Blah / CurrentVersion / MyCoolSubKey 的子项”

实际上是试图删除

HKCU/Blah/CurrentVersion/MyCoolSubKey

难怪找不到该子项。

虽然我认为您关于在注册表中不加选择地删除随机密钥的整个想法是完全有缺陷的,但谈到算法,您可以通过丢弃所有未使用的代码并使之保持简单来改进它,如下所示:

foreach (string s in Registry.CurrentUser.OpenSubKey("Software\\Microsoft").GetSubKeyNames())
{
    if (s.All(Char.IsDigit))
    {
        Registry.CurrentUser.DeleteSubKey($"Software\\Microsoft\\{s}");
    }
}

再次,我非常不喜欢以这种方式干预注册表的想法。

答案 1 :(得分:0)

完成!我发现错误。尝试直接删除子项时出现问题!

我添加了此代码

RegistryKey hkcus = Registry.CurrentUser; RegistryKey sware = hkcus.OpenSubKey(@"Software\\",true); RegistryKey msoft = sware.OpenSubKey(@"Microsoft\\",true); msoft.DeleteSubKey(Key2,true);