What status returned from RegistryKey.OpenSubKey?

  • Thread starter Thread starter Steve Bottoms
  • Start date Start date
S

Steve Bottoms

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
 
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
 
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
 
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/d...softwin32registrykeyclassopensubkeytopic1.asp

http://msdn.microsoft.com/library/d...softwin32registrykeyclassopensubkeytopic2.asp

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

Hope this helps
Jay
 
Yeah, I was using the wrong bloody test... Started using IsNothing() and it
works as expected now... :)

Thanks for the help! :)

Jack
 
Steve,

Steve Bottoms said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top