Solved

Searching through the registry with VB.NET

Posted on 2003-11-12
4
1,731 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 50 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

Suggested Solutions

Title # Comments Views Activity
Visual Studio hangs on running project 6 83
SSIS GUID Variable 2 56
Nested forach loop to linq 3 41
Make borderless form movable by user 2 26
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
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)…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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