A couple of problems that I can see.
Firstly readReg
is actually calling OpenSubKey
on Registry.CurrentUser
. That reads from HKCU
. You need to use from Registry.LocalMachine
for HKLM
. Pass the root key to readReg
as an extra parameter.
On top of that you will likely be caught out by the registry redirector. Your 32 bit process (well, I assume your process is 32 bit) is redirected to the 32 bit view of the registry. But the key you are looking for is in the 64 bit view of the registry.
In other words your attempt to access HKLM\Software
is being redirected to HKLM\Software\Wow6432Node
.
Use the RegistryView
enumeration to allow your 32 bit process to access the 64 bit view of the registry.
Your readReg
function should also be modified to check for errors in the call to OpenSubKey
. That method can return null
. You should deal with that scenario in a cleaner way then let a NullReferenceException
be thrown.