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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance 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
Bob LearnedCommented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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
rmmarshAuthor Commented:
I'm sorry... please split the points, giving Andy 350, and Bob (TheLearnedOne) the remainder.  I apologize for my screwup!

R
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyAinscowFreelance programmer / ConsultantCommented:
Thanks for the apology.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.