Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Searching through the registry with VB.NET

Posted on 2003-11-12
4
Medium Priority
?
1,741 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

609 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