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

Searching through the registry with VB.NET

I'm trying to develop an application, and part of it needs to determine the path of installed programs.  Is there a way to search through the registry with say a a user entered string.  Then return the installed path as a string?  I've got it now where I specify the location in the registry and it returns the installed path but it would be nice to allow the user to basically do a search for a program, and it will search the registry for those entries.

Thanks in advance.
0
LobbDoggy
Asked:
LobbDoggy
  • 2
  • 2
1 Solution
 
iboutchkineCommented:
That is very useful registry module. It will help to search through the reg

Module modReg

    Public Enum enRegRoot
        HKEY_CURRENT_USER = &H80000001
        HKEY_LOCAL_MACHINE = &H80000002
    End Enum

    Private Function fnSubKeyExist(ByVal regKey As Microsoft.Win32.RegistryKey, ByVal strSubKey As String) As Boolean
        ' Function to check if a sub key exists
        Dim blTmp As Boolean
        Dim intCnt As Integer, intTop As Integer
        blTmp = False
        intCnt = 0
        intTop = regKey.SubKeyCount
        Do Until blTmp Or intCnt = intTop
            If regKey.GetSubKeyNames(intCnt).ToLower = strSubKey.ToLower Then blTmp = True
            intCnt += 1
        Loop
        fnSubKeyExist = blTmp
    End Function

    Private Function fnValueExist(ByVal regKey As Microsoft.Win32.RegistryKey, ByVal strValueName As String) As Boolean
        ' Function to check if an item exists.
        Dim blTmp As Boolean = False
        Dim intCnt As Integer = 0, intTop As Integer = regKey.ValueCount
        'intTop = regKey.ValueCount
        Do Until blTmp Or intCnt = intTop
            If regKey.GetValueNames(intCnt).ToLower = strValueName.ToLower Then blTmp = True
            intCnt += 1
        Loop
        fnValueExist = blTmp
    End Function

    Private Function fnGetSubKey(ByVal regKey As Microsoft.Win32.RegistryKey, ByVal strKey As String) As Microsoft.Win32.RegistryKey
        ' Function to recurse to get sub key.
        Dim regTmp As Microsoft.Win32.RegistryKey
        If InStr(strKey, "\") > 0 Then
            regTmp = regKey.OpenSubKey(Left(strKey, InStr(strKey, "\") - 1))
            regTmp = fnGetSubKey(regTmp, Mid(strKey, InStr(strKey, "\") + 1))
        Else
            regTmp = regKey.OpenSubKey(strKey)
        End If
        fnGetSubKey = regTmp
    End Function

    Private Function fnGetAddSubKey(ByVal regKey As Microsoft.Win32.RegistryKey, ByVal strKey As String) As Microsoft.Win32.RegistryKey
        ' Function to recurse to get sub key and add it if it doesn't exist.
        Dim regTmp As Microsoft.Win32.RegistryKey
        If InStr(strKey, "\") > 0 Then
            If fnSubKeyExist(regKey, Left(strKey, InStr(strKey, "\") - 1)) Then
                regTmp = regKey.OpenSubKey(Left(strKey, InStr(strKey, "\") - 1), True)
                regTmp = fnGetAddSubKey(regTmp, Mid(strKey, InStr(strKey, "\") + 1))
            Else
                regTmp = regKey.CreateSubKey(Left(strKey, InStr(strKey, "\") - 1))
                regTmp = fnGetAddSubKey(regTmp, Mid(strKey, InStr(strKey, "\") + 1))
            End If
        Else
            regTmp = regKey.OpenSubKey(strKey)
            If fnSubKeyExist(regKey, strKey) Then
                regTmp = regKey.OpenSubKey(strKey, True)
            Else
                regTmp = regKey.CreateSubKey(strKey)
            End If
        End If
        fnGetAddSubKey = regTmp
    End Function

    Public Function fnGetRegKey(ByVal enRoot As enRegRoot, ByVal strKey As String, ByVal strItem As String) As String
        ' Function to get registry key item value.
        ' Example:  Get HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Cdrom, Autorun
        ' Usage:    fnGetRegKey(HKEY_LOCAL_MACHINE, "System\CurrentControlSet\Services\Cdrom", "Autorun")
        Dim regKey As Microsoft.Win32.RegistryKey
        Dim strTmp As String
        Select Case enRoot
            Case enRegRoot.HKEY_CURRENT_USER
                regKey = fnGetSubKey(Microsoft.Win32.Registry.CurrentUser, strKey)
            Case enRegRoot.HKEY_LOCAL_MACHINE
                regKey = fnGetSubKey(Microsoft.Win32.Registry.LocalMachine, strKey)
        End Select
        strTmp = regKey.GetValue(strItem)
        fnGetRegKey = strTmp
    End Function

    Public Function fnSaveRegKey(ByVal enRoot As enRegRoot, ByVal strKey As String, ByVal strItem As String, ByVal strVal As String)
        ' Function to save registry key item value.
        ' Example:  Set HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Cdrom, Autorun to 0
        ' Usage:    fnSaveRegKey(HKEY_LOCAL_MACHINE, "System\CurrentControlSet\Services\Cdrom", "Autorun", 0)
        Dim regKey As Microsoft.Win32.RegistryKey
        Select Case enRoot
            Case enRegRoot.HKEY_CURRENT_USER
                regKey = fnGetAddSubKey(Microsoft.Win32.Registry.CurrentUser, strKey)
            Case enRegRoot.HKEY_LOCAL_MACHINE
                regKey = fnGetAddSubKey(Microsoft.Win32.Registry.LocalMachine, strKey)
        End Select
        regKey.SetValue(strItem, strVal)
    End Function
End Module
0
 
LobbDoggyAuthor Commented:
Sorry for being such a newbie here, but how do I use the module.  I've created a new module in my program and have that code in there, but how do I specify what the user wants to look for?

0
 
iboutchkineCommented:
As you can see this module has different functions. You can call these functions from your program. Don't forget to make these functions Public if you have them in a separate module.
0
 
LobbDoggyAuthor Commented:
okay thanks very much.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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