Click here to Skip to main content
15,886,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to make an application in WinForm C# as my final programming project. The project is about managing Registry more nicely to make it easier for the user to edit values.
The problem is, when I read whether the UninstallString exists or not, the function for some reason doesn't look in the catch inside the try when it fails (and it fails since the app isn't 64-bit, thus the Registry value needs to be accessed differently)

public bool ValueExists(string Key, string Value)
    {
        try
        {
            try
            {
                RegistryKey rk = Registry.LocalMachine.OpenSubKey(Key);
                return rk.GetValue(Value) != null; //Error happens here when selected 64-bit application. System.NullReferenceException: 'Object reference not set to an instance of an object.'
            }
            catch (NullReferenceException ex)
            {
                RegistryKey regkey64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
                RegistryKey rk64 = regkey64.OpenSubKey(Value);
                return regkey64.OpenSubKey(Key).GetValue(Value) != null;
            }
        }
        catch
        {
            return false;
        }
    }


System.NullReferenceException: 'Object reference not set to an instance of an object.' - This is the error and I KNOW it happens cause I select a 64-bit application, but for some reason it ignores the catch.

What I have tried:

try
            {
                RegistryKey regkey64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
                RegistryKey rk64 = regkey64.OpenSubKey(Value);
                return regkey64.OpenSubKey(Key).GetValue(Value) != null;
            }
            catch (NullReferenceException ex)
            {
                RegistryKey rk = Registry.LocalMachine.OpenSubKey(Key);
                return rk.GetValue(Value) != null;
            }


If I swap the order so when I select a 64-bit Registry value it doesn't crash, but when I select a 32-bit Registry value it crashes with the same error.
rk is null under the right circumstances - which means if the selected 32-bit Registry value doesn't exists. But again, if the selected Registry value is 64-bit it'd be unreadable, thus I need to use the code in the try inside the catch (to check whether a 64-bit Registry value exists or not).
Posted
Updated 4-Nov-20 23:30pm

You're potentially throwing another NullReferenceException from within your first catch block.

But you don't need either of those catch blocks:
C#
public bool ValueExists(string Key, string Value)
{
    using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(Key, false))
    {
        if (rk != null) return rk.GetValue(Value) != null;
    }
    
    if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
    {
        using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
        using (RegistryKey rk = hklm.OpenSubKey(Value, false))
        {
            if (rk != null) return rk.GetValue(Value) != null;
        }
    }
    
    return false;
}
 
Share this answer
 
v2
Comments
WillingMost7 5-Nov-20 5:57am    
Thank you SOOOOOOOOOO much!!! That worked indeed.
Though I had to make a change in the second if you wrote (cause there was a bug it showed wrong output).

I kept the second if but replaced everything inside with this:
RegistryKey regkey64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
return regkey64.OpenSubKey(Key).GetValue(Value) != null;
Why are you relying on a NullReferenceException to check the code is correct. You should test the returned value from any of the Registry calls to see if it is null before proceeding. You should never write code like:
C#
RegistryKey rk = Registry.LocalMachine.OpenSubKey(Key);
return rk.GetValue(Value) != null;

Since the returned value from OpenSubKey may be null.
 
Share this answer
 
Comments
WillingMost7 5-Nov-20 5:26am    
If the value I'm looking for exists then it's not null
Richard MacCutchan 5-Nov-20 5:27am    
Well that is pretty obvious.
WillingMost7 5-Nov-20 5:28am    
Ok, so how else do you suggest to write it?
Richard MacCutchan 5-Nov-20 5:45am    
I already suggested it: always check return values from system calls to see whether they are null.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900