Solved

Check If Registry SubKey Exist Or Not.

Posted on 2006-11-05
16
607 Views
Last Modified: 2008-01-09

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
Comment
Question by:Zak-R
  • 8
  • 7
16 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17875912
I think you might want to use existing utility code like this (assuming vb6):
http://www.codetoad.com/vb_modify_registry.asp


0
 
LVL 29

Expert Comment

by:Nightman
ID: 17875915
0
 
LVL 2

Author Comment

by:Zak-R
ID: 17875922

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
 
LVL 2

Author Comment

by:Zak-R
ID: 17875938


=================================
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
 
LVL 29

Expert Comment

by:Nightman
ID: 17875997
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
 
LVL 29

Expert Comment

by:Nightman
ID: 17876010
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
 
LVL 2

Author Comment

by:Zak-R
ID: 17876014

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
 
LVL 29

Expert Comment

by:Nightman
ID: 17876020
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 2

Author Comment

by:Zak-R
ID: 17876029

Yes, exactly just the SubFolder.


0
 
LVL 29

Expert Comment

by:Nightman
ID: 17876035
Try the code I just posted - it returns true or false to the textbox based on the existence of that folder.
0
 
LVL 2

Author Comment

by:Zak-R
ID: 17876039

Yeah, I been trying to get it to work, But it does not show any False or True in the TextBox.
0
 
LVL 29

Accepted Solution

by:
Nightman earned 50 total points
ID: 17876044
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
 
LVL 2

Author Comment

by:Zak-R
ID: 17876045

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

Thank you
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17876052
Remember, you can use that class to modify, add, delete, check for existence or enumerate values from the registry - pretty useful.
0
 
LVL 2

Author Comment

by:Zak-R
ID: 17876055

Also thank you for you help angelIII
0
 
LVL 2

Author Comment

by:Zak-R
ID: 17876057

Thanks Nightman, I'll play with it : )

Thank you
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now