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;
}
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).