Link to home
Start Free TrialLog in
Avatar of joshuaanderson
joshuaanderson

asked on

Recursive search Through the registry

I am trying to search for a particular value in the registry. How can a have vb.net recursively search values through the registry?

Thanks!
Avatar of armoghan
armoghan
Flag of Pakistan image

This is a code in VB
http://www.freevbcode.com/ShowCode.Asp?ID=3175

can be converted to VB.NET
Avatar of joshuaanderson
joshuaanderson

ASKER

Well, I figured it out. I appreciate comments. Here is real VB.net code that worked to recursively find a value in the registry.
I have not seen any VB.net code like this. So hopefully this will help out others.

Imports System
Imports Microsoft.Win32
Imports System.Threading

Public Class Form1
    Inherits System.Windows.Forms.Form
    Public Shared ProcessThread As Thread
    Dim Path As String = "System\\ControlSet001\\Control\\Network\\"
    Dim SearchStr As String = "Name = Local Area Connection"

  Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ProcessThread = New Thread(AddressOf SearchThread)
        ProcessThread.Start()
    End Sub

    Sub SearchThread()
        Search(Path, SearchStr)
    End Sub

    Public Sub Search(ByVal Path As String, ByVal SearchStr As String)
        Dim ParentKey As RegistryKey = Registry.LocalMachine.OpenSubKey(Path, True)
        ' Loop through values in the subkey
        For Each valueName As String In ParentKey.GetValueNames()
            On Error Resume Next
            'Create String to Compare against
            Dim CurStr As String = valueName + " = " + ParentKey.GetValue(valueName)
            ListBox1.Items.Insert(0, valueName + " = " + ParentKey.GetValue(valueName))
            If CurStr = SearchStr Then
                MsgBox(CurStr)
                CurStr = ""
            End If
        Next
        'if there are sub keys loop through and be recursive
        If ParentKey.SubKeyCount > 0 Then
            For Each subKeyName As String In ParentKey.GetSubKeyNames()
                ListBox1.Items.Insert(0, "")
                ListBox1.Items.Insert(0, Path + subKeyName) ' Writing the Keyname
                'This is what makes me recursive!
                Dim Thispath As String = Path + subKeyName + "\\"
                Search(Thispath, SearchStr)
            Next
        End If

    End Sub

End Class
its nice to hear that you got the solution
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial