When I try to delete a key in HKCU that has subkeys I get an error.
Here is the code I am using:
using (RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"Software\Policies\", true))
{
if (regkey.OpenSubKey("Google") != null)
{
regkey.DeleteSubKey("Google");
}
}
The error I get:
Registry key has subkeys and recursive removes are not supported by this method.
How can I overcome it?
Use the RegistryKey.DeleteSubKeyTree
method.
RegistryKey.DeleteSubKeyTree Method (String)
Deletes a subkey and any child subkeys recursively.
using(RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"Software\Policies\", true))
{
if (regkey.OpenSubKey("Google") != null)
{
regkey.DeleteSubKeyTree("Google");
}
}
using(var regkey = Registry.CurrentUser.OpenSubKey(@"Software\Policies\", true))
{
regkey?.DeleteSubKeyTree("Google");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With