?
Solved

Searching through the registry with VB.NET

Posted on 2003-11-12
4
Medium Priority
?
1,737 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
iboutchkine earned 150 total points
ID: 9739472
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
ID: 9740287
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
ID: 9740831
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
ID: 9743011
okay thanks very much.
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 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