Search code examples
c#registrydirectory

deletesubkeytree not deleting registry folder


I want to delete a folder named EXAMPLE and all values in it in registry that is found under "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\EXAMPLE"

i have tried this ( with true at the end aswell )

Microsoft.Win32.Registry.LocalMachine.DeleteSubKeyTree(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\EXAMPLE");

and tried this

string keyName = @"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";

using (RegistryKey key = Registry.LocalMachine.OpenSubKey(keyName, true))
{
    key.DeleteSubKeyTree("EXAMPLE", true);
}

They either throw me a null exception or argument exception. How can i just delete an entire folder with all its keys ? So that i can have the similar effect from batch : REG DELETE "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\EXAMPLE" /f


Solution

  • The path is incorrect, Registry.LocalMachine doesn't have such a sub key @"HKEY_LOCAL_MACHINE\...". You need to start from "SOFTWARE\..".

    string keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
    

    And from your feedback, your program is affected by the Registry Redirector, in which HKEY_LOCAL_MACHINE\Software is redirected to HKEY_LOCAL_MACHINE\Software\Wow6432Node for a 32-bit program, recompile your program from AnyCPU (Prefer 32-bit) to x64 gets rid of the redirection.

    The mechanism of Registry Redirector varies on different Windows versions, so I leave this to you for further reading to understand why 64/32 matters in this case.