0

I need to check, with my VB.net software, if a program has been installed before the execution of my software. I've looked around and I've found that I can make this by inspecting the Registry Editor in Windows. But how can I detect the installation path of this software? I mean, in my case the software is installed on the drive D and not on the C one so, if I map the C path, I'll receive an error. How could I get the sure installation path of it? Hoping to have been as clearer as possible, thanks all are gonna answer me. Best regards

  • See [Using Windows Installer to Inventory Products and Patches](https://docs.microsoft.com/en-us/windows/win32/msi/inventory-products-and-patches-?redirectedfrom=MSDN) or [Win32_Product class](https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/aa394378(v=vs.85)) and [Working with Software Installations](https://docs.microsoft.com/en-us/powershell/scripting/samples/working-with-software-installations?view=powershell-7.1) (quite slow in this task). A [PoweShell alternative](https://stackoverflow.com/q/25268491/7444103) (Registry lookup) - All need admin privileges. – Jimi Mar 05 '21 at 11:41
  • I've tried Win32_produtct but the software I'm looking for isn't in the list. I've also tried Win32_service and its License Manager appears but it's not what I'm looking for. Is there a way to understand the installation path from the Registry Key Computer\HKEY_CURRENT_USER\Software\ANSYS, Inc.\ANSYS? This is the key I'm using to detect if the software has been previously installed on the PC. – telemaco10399 Mar 05 '21 at 14:57
  • I've tried also to look for it in Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ with no success – telemaco10399 Mar 05 '21 at 15:06

1 Answers1

0

Here are two examples of how to check a couple of things.

First, this is how one of my tools checks to see what version of itself is already installed. This isn't exactly what you're looking for, but don't worry - I'll get to that in just a minute:

Public Function GetSolutionVersion(MyApp As String)
    Dim uninstallKey As String = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    Using rk As RegistryKey = Registry.LocalMachine.OpenSubKey(uninstallKey)
        For Each skName In rk.GetSubKeyNames
            Dim name As String = Registry.LocalMachine.OpenSubKey(uninstallKey).OpenSubKey(skName).GetValue("DisplayName")
            If Not name Is Nothing Then
                If name.ToString.ToLower = MyApp.ToLower Then
                    Try
                        Dim displayversion As String = Registry.LocalMachine.OpenSubKey(uninstallKey).OpenSubKey(skName).GetValue("DisplayVersion")
                        Return displayversion
                    Catch ex As Exception
                        Return ""
                    End Try
                End If
            End If
        Next
    End Using
    Return ""
End Function

If you actually browse some of the entries in Uninstall, you'll see that some of them - but definitely not all - have an InstallLocation key that tells you right where to find the program. It depends on their particular installer. You may also be able to figure out the path by looking at the UninstallPath or UninstallString keys; again, it depends their installer.

As a second example, let's say we want to see if Firefox is installed - and where:

Private Sub CheckFirefox()
    'Dim readValue = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\MyApp", "Name", Nothing)
    'Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe
    
    Dim HasFirefox as Boolean = False

    Try
        Dim regvalue = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe", "", Nothing)
        If regvalue <> Nothing Then
            'Found firefox!
            FirefoxFullPath = regvalue
            HasFirefox = File.Exists(FirefoxFullPath)
            If HasFirefox = False Then
                MsgBox($"WARNING!  Registry says Firefox is at ({regvalue}), but no such file was found")
            Else
                MsgBox($"Firefox found at {FirefoxFullPath}")
            End If
        Else
            MsgBox("Firefox path not found in registry")
            HasFirefox = False
        End If
        
        If HasFirefox = True Then
            
            'Do something...
            
        End If

    Catch ex As Exception        
        MsgBox($"Unable to find Firefox in registry because an error occurred: {ex.ToString}",, "Error")
        HasFirefox = False
    End Try

End Sub

Knowing the right place to look in the Registry and how to get the right key values is the hardest part of it - hope this helps. However, not every piece of software is going to be listed in App Paths - it depends on whether they went through the registration process.

Finally, if none of that works, think about how the user is going to launch this particular program. If they're going to use either a desktop shortcut or menu entry, you could scan for those and retrieve its properties.

technonaut
  • 484
  • 3
  • 12