3
votes

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?

2
I think your path should be \Software\Microsoft\Windows\CurrentVersion\test, since you are already in the HKCU from Registry.CurrentUser so it is looking for HKEY_CURRENT_USER\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\test - vcsjones
@vcsjones: why don't you post that as an answer (without the first backslash)? - Mat
oh my gosh ... I copied it over incorrectly - you're right ... - user725913
@Mat Wasn't able to test my theory, and I wasn't sure enough to make it an answer. If it helps, awesome. - vcsjones

2 Answers

2
votes

Could it be that you are on a 64 bit machine and your project is set to x86 architecture? in that case, verify that the key you state exists under HKCU\Software\Wow6432Node... as every path is redirected to this 32 bit process registry...

1
votes

You should not include HKEY_CURRENT_USER in the string you pass to Registry.CurrentUser.OpenSubKey(). Instead use

string regPath_Key = @"Software\Microsoft\Windows\CurrentVersion\test";