Solved

Check If Registry SubKey Exist Or Not.

Posted on 2006-11-05
16
608 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
z = x + y – 1 6 67
DO Loop not working 4 72
Question about INSTR  - need to extract data MS ACCESS query 6 53
Visual Studio 2005 text editor 10 33
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

943 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

10 Experts available now in Live!

Get 1:1 Help Now