Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Subkey error (C#)

Tags:

c#

key

registry

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?


1 Answers

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...

like image 64
eFloh Avatar answered Jan 30 '26 15:01

eFloh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!