Solved

Searching through the registry with VB.NET

Posted on 2003-11-12
4
1,722 Views
Last Modified: 2008-03-18
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
Comment
Question by:LobbDoggy
  • 2
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
iboutchkine earned 50 total points
Comment Utility
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
 

Author Comment

by:LobbDoggy
Comment Utility
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
 
LVL 28

Expert Comment

by:iboutchkine
Comment Utility
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
 

Author Comment

by:LobbDoggy
Comment Utility
okay thanks very much.
0

Featured Post

What Security Threats Are You Missing?

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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