• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 624
  • Last Modified:

Check If Registry SubKey Exist Or Not.


I am trying to see if i can make something in VB that
can tell me if a SubKey in Registry does exist or not.

Dim strRegResult As String
Read = ReadRegistry
If "HKEY_CURRENT_USER\SOFTWARE\SubTest\" exist Then
MsgBox "It does exist"
Else
MsgBox "The SubKey does not exist"
End If

Sorry i just made all that up to give some small example of what i want.

Thanks
0
Zak-R
Asked:
Zak-R
  • 8
  • 7
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I think you might want to use existing utility code like this (assuming vb6):
http://www.codetoad.com/vb_modify_registry.asp


0
 
Zak-RAuthor Commented:

Those two links are good,
But i just want to get the reading value of the SubKey.
Note that i don't want to read any Stream value of the Registry.

Take a look to this example:

Private Sub Form_Load()
Text1.Text = GetString(HKEY_CURRENT_USER, "Software\SubTest", "MyStreamValue")
End Sub

With this i can get the value of a Stream Key but not a the SubKey.
Get it ?



0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Zak-RAuthor Commented:


=================================
Thanks angel, but the example in this page:
http://www.codetoad.com/vb_modify_registry.asp
does not have a solution, it does not work for me.
=================================

Note: I am not looking for a code to modefy the Registry,
I just  want my app to determine if the SubKey exist or not.

Thanks

0
 
NightmanCTOCommented:
In the example that I posted, add the cRegistry.cls as a class in your project.

Then:

Private Sub Form_Load()
  Dim oReg As cRegistry
  Set oReg = New cRegistry
  oReg.ClassKey = HKEY_CURRENT_USER
  oReg.SectionKey = "Software\SubTest"
  oReg.ValueKey = "MyStreamValue"
  Text1.Text = oReg.Value
End Sub

There is also a method called KeyExists - you can use this to test for the existence of a given key.
0
 
NightmanCTOCommented:
obviously don't forget to set oReg=nothing at the end

Alternatively you could create the oReg class object at a modular level and keep it in memory throughout the lifespan of you application as well.
0
 
Zak-RAuthor Commented:

Hi Nightman

Read my comment again, I really apreciate you reply, but i mentioned
that i don't want to read the Stream Key from this path in the Registry.

*** I want to be able to get in the TextBox if the that SubKey exist or not. ***

Dim oReg As cRegistry
Set oReg = New cRegistry
oReg.ClassKey = HKEY_CURRENT_USER
oReg.SectionKey = "Software\SubTest"  <=== Just this SubKey
oReg.ValueKey = "MyStreamValue"        <=== But not this one
Text1.Text = oReg.Value

I will apreciate if you can help me with it

Thanks






0
 
NightmanCTOCommented:
Dim oReg As cRegistry
Set oReg = New cRegistry
oReg.ClassKey = HKEY_CURRENT_USER
oReg.SectionKey = "Software\SubTest"  <=== Just this SubKey
Text1.Text = CStr(oReg.KeyExists)

0
 
Zak-RAuthor Commented:

Yes, exactly just the SubFolder.


0
 
NightmanCTOCommented:
Try the code I just posted - it returns true or false to the textbox based on the existence of that folder.
0
 
Zak-RAuthor Commented:

Yeah, I been trying to get it to work, But it does not show any False or True in the TextBox.
0
 
NightmanCTOCommented:
odd, it works for me.

Private Sub Form_Load()
  Dim oReg As cRegistry
  Set oReg = New cRegistry
  oReg.ClassKey = HKEY_CURRENT_USER
  oReg.SectionKey = "Software\SubTest"
  Text1.Text = CStr(oReg.KeyExists)
End Sub

Is there anything else that you might be doing to clear this value, or set it to something else?
0
 
Zak-RAuthor Commented:

Oh Yeah!, this time works : )
      Thanks Nightman.
This is now what i was looking for, but i think is good.

Thank you
0
 
NightmanCTOCommented:
Remember, you can use that class to modify, add, delete, check for existence or enumerate values from the registry - pretty useful.
0
 
Zak-RAuthor Commented:

Also thank you for you help angelIII
0
 
Zak-RAuthor Commented:

Thanks Nightman, I'll play with it : )

Thank you
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now