Solved

Check If Registry SubKey Exist Or Not.

Posted on 2006-11-05
16
612 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 143

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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Macro which automatically sends attachment to Outlook 14 78
vb6 connector to mongodb 2 122
Modifying Conditional Format from VBA code 3 60
TT Auto Dashboard 13 95
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

820 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