Check to see if registry key exists

svnkl

New member
Joined
Sep 15, 2006
Messages
2
Programming Experience
1-3
Hi all,

I am trying to figure out how to check if a registry key exists. If it does, do nothing, if it doesn't, recreate it.

I've found numerous resources on the subject, and have made progress, but one little thing is stoping me.

Here is my code that I have so far:
VB.NET:
Dim regKey As Microsoft.Win32.RegistryKey

regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\application\regkey", True)

If regKey Is Nothing Then            
  MsgBox("Not Found")
Else
  MsgBox("Found")
End If
The problem is that this reports "Not Found", even though the key 'regkey' exists in the registry. Also, if I remove 'regkey' from the code, it reports "Found". So it does find the subdirectory in the registry.

My question is, how do I check to see if the 'regkey' exists?

I'm running Windows XP Pro SP2. Thanks.
 
Last edited:
Are you sure your '.....\regkey' is a registry key and not a valuename?

You see if you open Registry Editor the registry keys is displayed in the treeview at left and containing name/value pairs in the listview at right.

If your '.....\regkey' happens to be a value name you first open the registry key "SOFTWARE\application" then use the GetValue("name") and check if the return is nothing.

You could also inspect the confirmed path with the methods GetSubKeyNames and GetValueNames, they both return an array of strings for review.
 
I told you so... :) Your '.....\regkey' wasn't a registry key, it was the value name.
 
Back
Top