Search code examples
c#registryregistrykeydelete-method

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. 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

testKey2 is now assigned to 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. 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. 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." It then lists all the subkeys under HKEY_CURRENT_USER, which still includes the 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. I'd rather just be able to invoke testKey2 as a parameter and delete the key that way. Is there a way to do that?


Solution

  • 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". Then, the only other alteration needed is to the delete function's parameter.

    Registry.CurrentUser.DeleteSubKeyTree(testKey2);
    

    removing the ToString() method.

    I believe the problem was as I said, that testKey2 was getting "HKEY_CURRENT_USER//SubKeyTester" instead of just "SubKeyTester". But this way, testKey2 only gets "SubKeyTester", which allows for a correct filepath to the appropriate key.

    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.

    Also, this solution does not require a .close() statement, because no key was ever opened.