Can registry keys be deleted only in one level up?
-
12-06-2021 - |
Question
In our application we are having different components and registry key will be created in component Name.
For ex.
We are having a component named Notification and we are having registry key as follows
RegistryKey rKeyNotification = Registry.LocalMachine.OpenSubKey("SOFTWARE\\CompanyName\\ProdName\\Notification");
Now i wanted to delete this key and all the subkeys.
I was trying to delete this rKeyNotification completely. But i don't find any way.
I was trying like follows.
if (rKeyNotification != null) {
rKeyNotification.DeleteSubKeyTree("Notification");
}
But this piece will look for RegistryKey rKeyNotification =Registry.LocalMachine.OpenSubKey("SOFTWARE\\CompanyName\\ProdName\\Notification\\Notification");
There is no option like rKeyNotification.DeleteSubKeyTree()
Should i always go up one level (Registry.LocalMachine.OpenSubKey("SOFTWARE\\CompanyName\\ProdName)
then only delete the key?
Solution
There is no method to delete a key itself, but you can use Registry.LocalMachine
directly:
string keyname = @"SOFTWARE\CompanyName\ProdName\Notification";
Registry.LocalMachine.DeleteSubKeyTree(keyname);