I have created the following registry key (copied through regedit):
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\test
I would like to now delete this registry key, and so… I have been using the following code and am running into a small error.
RegistryKey regKey;
string regPath_Key = @"Software\Microsoft\Windows\CurrentVersion\test";
regKey = Registry.CurrentUser.OpenSubKey(regPath_Key, true);
if(regKey != null) // Always returns null, even though the key does exist.
{
Registry.CurrentUser.DeleteSubKey(regPath_Key, true);
}
The issue I am having is that the line if(regKey != null) always returns null! I have gone back and checked that the key does in fact exist multiple times – but still the same result. I am going to assume my code has issues somewhere?
You should not include
HKEY_CURRENT_USERin the string you pass toRegistry.CurrentUser.OpenSubKey(). Instead use