Link to home
Start Free TrialLog in
Avatar of rmmarsh
rmmarshFlag for United States of America

asked on

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

SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rmmarsh

ASKER

No, I set it using this code:

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

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rmmarsh

ASKER

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...
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the apology.