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

What status returned from RegistryKey.OpenSubKey?

P: n/a
Hi, all... Trying to determine how to check the state of OpenSubKey when
called. What is the return value if the requested subkey doesn't exist?? I
try a GetType on the result but only get a bizarre error, so I'm uncertain
what should be returned on both success or failure!

Thanks!
Jack
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
What is the return value if the requested subkey doesn't exist??
Nothing

I try a GetType on the result but only get a bizarre error, so I'm uncertain
what should be returned on both success or failure!


You have to ensure that the return value isn't Nothing before trying
to call GetType on it.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2

P: n/a
Hmmm... I just tried an OpenSubKey on a key I know doesn't exist, and the
return is NOT null... ?? Same for a key that I know exists, so testing for
Null on the return isn't a valid test.

Is there a valid test? I try a GetType on the non-null return and get
another error, so getType doesn't work on whatever is returned.

Suggestions?
Jack
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
What is the return value if the requested subkey doesn't exist??


Nothing

Nov 21 '05 #3

P: n/a
Jack,
Hmmm... I just tried an OpenSubKey on a key I know doesn't exist, and the
return is NOT null... ?? Remember that C#'s "null" is the same as VB.NET's Nothing. Not to be
confused with DBNull or an SQL Null.
Can you post the code you are attempting to use to determine if the Registry
Key exists? (the code that is not working for you).

As Mattias states, RegistryKey.OpenSubKey returns Nothing if the subkey
does not exist.
Can you post how you are attempting to use GetType, as there is both a
GetType keyword and a GetType method.

Dim key As RegistryKey
Dim type As Type = GetType(System.String)
type = key.GetType()

For details on OpenSubKey see:

http://msdn.microsoft.com/library/de...bkeytopic1.asp

http://msdn.microsoft.com/library/de...bkeytopic2.asp

For an example to determining whether a registry key exists:
http://msdn.microsoft.com/library/de...ykeyexists.asp

Hope this helps
Jay

"Steve Bottoms" <sp**********@hotmail.com> wrote in message
news:Oy**************@TK2MSFTNGP14.phx.gbl... Hmmm... I just tried an OpenSubKey on a key I know doesn't exist, and the
return is NOT null... ?? Same for a key that I know exists, so testing
for Null on the return isn't a valid test.

Is there a valid test? I try a GetType on the non-null return and get
another error, so getType doesn't work on whatever is returned.

Suggestions?
Jack
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
What is the return value if the requested subkey doesn't exist??


Nothing


Nov 21 '05 #4

P: n/a
Yeah, I was using the wrong bloody test... Started using IsNothing() and it
works as expected now... :)

Thanks for the help! :)

Jack
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Jack,
Hmmm... I just tried an OpenSubKey on a key I know doesn't exist, and the
return is NOT null... ??

Remember that C#'s "null" is the same as VB.NET's Nothing. Not to be
confused with DBNull or an SQL Null.
Can you post the code you are attempting to use to determine if the
Registry Key exists? (the code that is not working for you).

As Mattias states, RegistryKey.OpenSubKey returns Nothing if the subkey
does not exist.
Can you post how you are attempting to use GetType, as there is both a
GetType keyword and a GetType method.

Dim key As RegistryKey
Dim type As Type = GetType(System.String)
type = key.GetType()

For details on OpenSubKey see:

http://msdn.microsoft.com/library/de...bkeytopic1.asp

http://msdn.microsoft.com/library/de...bkeytopic2.asp

For an example to determining whether a registry key exists:
http://msdn.microsoft.com/library/de...ykeyexists.asp

Hope this helps
Jay

"Steve Bottoms" <sp**********@hotmail.com> wrote in message
news:Oy**************@TK2MSFTNGP14.phx.gbl...
Hmmm... I just tried an OpenSubKey on a key I know doesn't exist, and the
return is NOT null... ?? Same for a key that I know exists, so testing
for Null on the return isn't a valid test.

Is there a valid test? I try a GetType on the non-null return and get
another error, so getType doesn't work on whatever is returned.

Suggestions?
Jack
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...

What is the return value if the requested subkey doesn't exist??

Nothing



Nov 21 '05 #5

P: n/a
Steve,

"Steve Bottoms" <sp**********@hotmail.com> schrieb:
Yeah, I was using the wrong bloody test... Started using IsNothing() and
it works as expected now... :)


I prefer 'If ... Is Nothing Then...' for reference types.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.