Invalid cast while trying to read registry entry

I have the following code that is giving me the above error.  Any idea why?  how do I fix it?  ( I have verified the value is "True")
RegistryKey networkStore = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Prager\\networkStore");
            bool regKey = networkStore == null? false : (bool)networkStore.GetValue("networked");

Open in new window

rmmarshAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
rmmarshConnect With a Mentor Author Commented:
I'm sorry... please split the points, giving Andy 350, and Bob (TheLearnedOne) the remainder.  I apologize for my screwup!

R
0
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
>>I have verified the value is "True"

Do you mean the key has a string value that is the string "True" ?


I'd also split the statement into smaller chunks for readability:

RegistryKey networkStore = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Prager\\networkStore");
            bool regKey = false;
if( networkStore != null)
  bRegKey =  (bool)networkStore.GetValue("networked");
0
 
rmmarshAuthor Commented:
No, I set it using this code:

                using (RegistryKey networkStore = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Prager\\networkStore\\"))
                    networkStore.SetValue("networked", true);

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Bob LearnedConnect With a Mentor Commented:
I would look at the key with REGEDIT to see what the key type is.  If it is REG_SZ, that is a string type.
0
 
rmmarshAuthor Commented:
Because the value is stored as "True", it has to be converted to boolean.

            bool regKey = networkStore == null? false : Convert.ToBoolean(networkStore.GetValue("networked"));

This works like a champ.  Thanks for your help anyway...
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Thanks for the apology.
0
All Courses

From novice to tech pro — start learning today.