简体   繁体   English

如何删除注册表项:获取错误“无法删除...因为该子项不存在”

[英]How to delete registry key: getting error “Cannot delete… because the subkey does not exist”

To begin, I recognize that this question appears similar to others. 首先,我认识到这个问题与其他问题类似。 However, my research thus far has not found a solution to the specific problem I am facing, just a lot of solutions to problems with similar circumstance. 然而,到目前为止,我的研究还没有找到解决我所面临的具体问题的方法,只是针对类似情况的问题的很多解决方案。

I am new to registry functions, so I've been using a VM to mess around with them and see what I can do. 我是注册表函数的新手,所以我一直在使用虚拟机来搞乱它们,看看我能做些什么。 Currently, I am trying to test the creation, reading, and subsequent deletion of a subKey and HKEY_CURRENT_USER. 目前,我正在尝试测试subKey和HKEY_CURRENT_USER的创建,读取和后续删除。 At the moment, I can do everything in that list except deletion. 目前,除了删除之外,我可以在该列表中执行所有操作。 The relevant code is as follows: 相关代码如下:

//This first sample will attempt to create a test key under HKEY_CURRENT_USER
        Console.WriteLine("Creating subkey under HKEY_CURRENT_USER");
        RegistryKey testKey2 = Registry.CurrentUser.CreateSubKey("SubKeyTester");
        Console.WriteLine("testKey2 is now assigned to {0}", testKey2);

        //This ensures that testKey2 is the value that I think it is
        Console.WriteLine("testKey2 value = {0}\n", testKey2);

with an output of: 输出:

Beginning test... Creating subkey under HKEY_CURRENT_USER 开始测试...在HKEY_CURRENT_USER下创建子项

testKey2 is now assigned to HKEY_CURRENT_USER\\SubKeyTester testKey2现在已分配给HKEY_CURRENT_USER \\ SubKeyTester

testKey2 value = HKEY_CURRENT_USER\\SubKeyTester testKey2 value = HKEY_CURRENT_USER \\ SubKeyTester

Notably, testKey2 has stored "HKEY_CURRENT_USER\\SubKeyTester" rather than the "SubKeyTester" that I expected. 值得注意的是,testKey2存储了“HKEY_CURRENT_USER \\ SubKeyTester”而不是我期望的“SubKeyTester”。 After this, I'm able to check the subkeys under HKEY_CURRENT_USER and verify that yes, "SubKeyTester" is indeed present among the CurrentUser subkeys. 在此之后,我能够检查HKEY_CURRENT_USER下的子键并验证是,“SubKeyTester”确实存在于CurrentUser子键中。 Now, I just need to delete it. 现在,我只需删除它。 My code is as follows: 我的代码如下:

 //This portion of the test will attempt to delete SubKeyTester from 
        //HKEY_CURRENT_USER
        Console.WriteLine("Attempting to delete test subkey\n");
        try
        {
            Registry.CurrentUser.DeleteSubKeyTree(testKey2.ToString());
            Console.WriteLine("Target has been deleted\n");
        }
        catch(Exception e)
        {
            Console.WriteLine("The key targeted for deletion... is not found.\nError: {0}\n", e);
        }

        //Another safety check to verify that only SubKeyTester has been deleted
        Console.WriteLine("There are {0} subkeys under {1}.",
            Registry.CurrentUser.SubKeyCount.ToString(), Registry.CurrentUser.Name);
        foreach (string subKeyName in Registry.CurrentUser.GetSubKeyNames())
            Console.WriteLine(subKeyName);

        testKey2.Close();

The output informs me: "Error: System.ArgumentException: Cannot delete a subkey tree because the subkey does not exist." 输出通知我:“错误:System.ArgumentException:无法删除子项树,因为该子项不存在。” It then lists all the subkeys under HKEY_CURRENT_USER, which still includes the testKey "SubKeyTester". 然后它列出了HKEY_CURRENT_USER下的所有子键,它们仍包含testKey“SubKeyTester”。

I believe the problem could be solved by just hard-coding the path to that subkey in the DeleteSubKeyTree call, but I want to avoid that. 我相信这个问题可以通过在DeleteSubKeyTree调用中硬编码该子键的路径来解决,但我想避免这种情况。 I'd rather just be able to invoke testKey2 as a parameter and delete the key that way. 我宁愿只能调用testKey2作为参数并以这种方式删除密钥。 Is there a way to do that? 有没有办法做到这一点?

I have found my error, and I was on the right track. 我发现了我的错误,而且我在正确的轨道上。 The correct code is as follows: 正确的代码如下:

    Console.WriteLine("Creating subkey under HKEY__CURRENT_USER\n");
    const string testKey2 = "SubKeyTester";
    Registry.CurrentUser.CreateSubKey(testKey2);

This way, testKey2 is always "SubKeyTester". 这样,testKey2始终是“SubKeyTester”。 Then, the only other alteration needed is to the delete function's parameter. 然后,唯一需要的其他更改是删除函数的参数。

Registry.CurrentUser.DeleteSubKeyTree(testKey2);

removing the ToString() method. 删除ToString()方法。

I believe the problem was as I said, that testKey2 was getting "HKEY_CURRENT_USER//SubKeyTester" instead of just "SubKeyTester". 我相信问题就像我说的那样,testKey2获得了“HKEY_CURRENT_USER // SubKeyTester”,而不仅仅是“SubKeyTester”。 But this way, testKey2 only gets "SubKeyTester", which allows for a correct filepath to the appropriate key. 但是这样,testKey2只获得“SubKeyTester”,它允许正确的文件路径到适当的密钥。

I had tried using 我曾经尝试过使用过

Registry.DeleteSubKeyTree(testKey2.ToString());

to get around the pathing error, but "Registry" does not have a DeleteSubKeyTree method, so that simply didn't work. 绕过路径错误,但“注册表”没有DeleteSubKeyTree方法,所以这根本不起作用。

Also, this solution does not require a .close() statement, because no key was ever opened. 此外,此解决方案不需要.close()语句,因为没有打开任何键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM