Is it possible to delete all registry keys that can be deleted without being stopped by one undeletable key using C#? : When you use the Registry Editor GUI to delete keys and you delete a subkey tree with undeletable keys inside it (e.g. key in use, insufficient permission), it will try to delete what it can without the undeletable keys stopping the operation.

This does not seem to be the case with the Microsoft.Win32 library in C#.

Using an external program like reg.exe works, but I'm trying to look for a solution that does not require an external application.

private void DelHKLM()
        {
            try
            {
                using (RegistryKey desiredKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", true))
                {
                    desiredKey.DeleteSubKeyTree("Policies");
                }
            }
        }

The anticipated result is for the program to not give any exceptions and delete the subkey tree "Policies", but instead, it throws an "UnauthorizedAccessException" because the keys are in use.

Note: I am running my program as Administrator and I do have permissions to the key as demonstrated through the registry editor.

So, my question is: **Is there any way to achieve what the Registry Editor does in C#, preferably using native libraries?**