Changes in Registry ?

Gloops

Well-known member
Joined
Jun 30, 2022
Messages
137
Programming Experience
10+
Hello everybody,

On my previous machine, I had this to read a value in the registry:
Reading a value in the registry:
        private bool getReg()
        {
            object v = Registry.GetValue(
                    @"HKEY_CURRENT_USER\Console\SonsTest",
                    "JouerSelectReminder",
                    4
                );
            string strSelectReminder = v.ToString();
            double dwSelectReminder = double.Parse(strSelectReminder);
            return dwSelectReminder == 1;
        }

When receiving a new machine, I got a "variable not implemented" exception, and I had to write this:

Reading the registry, new version:
  private bool getReg()
        {
            object v = Registry.GetValue(
                    @"HKEY_CURRENT_USER\Console\SonsTest",
                    "JouerSelectReminder",
                    4
                );
            if (v == null)
            {
                //key noot present in the register
                return false;
            }
            else
            {
                string strSelectReminder = v.ToString();
                double dwSelectReminder = double.Parse(strSelectReminder);
                return dwSelectReminder == 1;
            }
        }

On the previous machine, if the key is absent, v receives the default value.
On the new one, it receives null, so parsing it to an integer raises an exception.

I needed a little time to understand what happened, at a moment I thought Registry had to be implemented, and it did not make sense.

I have a doubt about a comportment of the Registry object (and its GetValue method) that would be specific to a machine.

Were there any changes in .Net?

Oh, perhaps an element: the project was created targeting net5.0-windows
On the new machine, as this version of .Net was not installed, Visual studio proposed to migrate it to net6.0-windows, and I accepted.
 
Last edited:
If you look at the documentation, the default value (4) will only be returned if the key value does not exist. It will return null if the key does not exist. It looks like on your new machine, that key "HKEY_CURRENT_USER\Console\SonsTest" does not exist.
 
Right, it did not exist.
I created it, but still did not get the correct results.
Anyway, as you say yourself, "the default value (4) will only be returned if the key value does not exist".
That is right in .Net 5, but it seems that in .Net 6, if the key value does not exist, you get null, and you have to test whether the resulting value is null.
 
Docs for all versions say the same:
null if the subkey specified by keyName does not exist; otherwise, the value associated with valueName, or defaultValue if valueName is not found.
 
Worked correctly for me...
1669239173867.png
 
Back
Top Bottom