By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,441 Members | 998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,441 IT Pros & Developers. It's quick & easy.

Writing (and reading) boolean data to the registry

P: n/a
Bry
I'm having problems writing (and reading) boolean data to the registry.

// Write a boolean value to the registry
// I've not included the obvious bits of code in these samples
bool myBool = true;
myRegistryKey.SetValue("SomeValue", myBool);

This gives me a registry value with string data containing "True"

// Read my boolean data back from the registry
bool myBool;
myRegistryKey.GetValue("SomeValue");

This produces a run time error, "InvalidCastException" was unhandled

I understand why I'm getting the exception, but I'm not sure what I
need to do to achieve what I'm trying to do. Can anyone point out what
I'm missing?

Thanks.

Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi Bry,

First, I don't see why the code you posted would throw an exception:

bool myBool;
myRegistryKey.GetValue("SomeValue");

This code would not actually *do* anything. You are not assigning the return
value of the function to anything. In fact, the RegistryKey.GetValue()
method is not documented as throwing an InvalidCastException at all.

Now, I can see how the *following* code would throw an InvalidCastException:

bool MyBool = myRegistryKey.GetValue("SomeValue");

The RegistryKey.GetValue() method returns a type of object, not boolean. You
would have to cast it as boolean.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

"Bry" <br*********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
I'm having problems writing (and reading) boolean data to the registry.

// Write a boolean value to the registry
// I've not included the obvious bits of code in these samples
bool myBool = true;
myRegistryKey.SetValue("SomeValue", myBool);

This gives me a registry value with string data containing "True"

// Read my boolean data back from the registry
bool myBool;
myRegistryKey.GetValue("SomeValue");

This produces a run time error, "InvalidCastException" was unhandled

I understand why I'm getting the exception, but I'm not sure what I
need to do to achieve what I'm trying to do. Can anyone point out what
I'm missing?

Thanks.

Nov 17 '05 #2

P: n/a
Bry
D'oh! I've posted the code incorrectly. Thanks for pointing that out.

It should read

// Read my boolean data back from the registry
bool myBool;
myBool = (bool) myRegistryKey.GetValue("SomeValue");

The last line is the one that throws the InvalidCastException

Nov 17 '05 #3

P: n/a
Bry
Also, FWIW, I'm using .net framework v2.0

Nov 17 '05 #4

P: n/a
"Bry" <br*********@gmail.com> wrote:
// Read my boolean data back from the registry
bool myBool;
myBool = (bool) myRegistryKey.GetValue("SomeValue");


I think you need to use Convert.ToBool, rather than trying to *cast*
to a bool, because your "True" or "False" is presumably stored in the
Registry as a string literal.

P.
Nov 17 '05 #5

P: n/a
Bry
Thank you, that works great.. I can't believe I've never seen that
function before!

Nov 17 '05 #6

P: n/a
You can store both string and binary data in the Registry. If you input
binary data (boolean), you will get back binary data.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

"Paul E Collins" <fi******************@CL4.org> wrote in message
news:dk**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com...
"Bry" <br*********@gmail.com> wrote:
// Read my boolean data back from the registry
bool myBool;
myBool = (bool) myRegistryKey.GetValue("SomeValue");


I think you need to use Convert.ToBool, rather than trying to *cast* to a
bool, because your "True" or "False" is presumably stored in the Registry
as a string literal.

P.

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.