2

I've been using QGIS 3.16.x for a while. For development purposes for a plugin, I'm looking for the QGIS installation path.

For 3.16, I could check the registry-editor using: Computer\HKEY_LOCAL_MACHINE\SOFTWARE\QGIS 3.16\InstallPath

which resulted in C:\Program Files\QGIS 3.16


Another option is to check the registry for installation information. I used this command in PowerShell:

Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object {$_.DisplayName -match "QGIS 3.16" } | Select UninstallString

This resulted in C:\Program Files\QGIS 3.16\uninstall.exe, from which I derive the installation path by removing the \uninstall.exe suffix.

But recently I've installed 3.22.4, and found that there is no QGIS 3.22 registry entry in Computer\HKEY_LOCAL_MACHINE\SOFTWARE. When I try the other option using:

Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object {$_.DisplayName -match "QGIS 3.22" } | Format-List *

This results in null-values for fields like UninstallString (which were available for 3.16 installations).

I've tried to search the entire registry for other references using the ID (formatted like 00000000-0000-0000-0000-000000000000) and used for e.g. MsiExec, but no luck.

We install QGIS as local admins system-wide, so all users that login with their own user-account can use QGIS on that machine. We don't want to install or update the application for all individual users that share the same computer.

How can I programmatically find the installation directory for QGIS 3.22?

If possible, one solution for both 3.16 and 3.22 would be great.

2
  • QgsApplication.prefixPath()
    – user2856
    Feb 28, 2022 at 12:51
  • Thanks for your reply @user2856. Is there another way to get the InstallPath from the selected QGIS version on Windows machines? Not inside the application itself, but from another application of from the registry?
    – bond007
    Feb 28, 2022 at 13:04

1 Answer 1

1

Let me just start by saying that this exact issue bothered me for quite a while now aswell. After our clients upgraded their QGIS Version from 3.16 to 3.22 we could not programmatically detect the installation directory anymore. The recent installers don't seem to be writing these registry values such as InstallPath anymore...

Last week I finally had time to come up with the following solution involving a small Visual Basic Script:

First step will be to find out the Product Code used to identify the QGIS application which is installed on the machine. This can be achieved with a powershell command:

PS C:\> Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object {$_.DisplayName -match "QGIS 3.22" } | Select -expand PSChildName

{DEA3CAFB-7511-1014-A941-05998B569609}

Second step involves the VBScript shown below, which will iterate through all installed software components and finds the first component that belongs to the given Product Code of QGIS. The first found component could be any random file that is shipped with the QGIS installation. In my case it was C:\Program Files\QGIS 3.22.5\apps\Python39\Lib\site-packages\scipy\io\matlab\tests\data\teststring_6.1_SOL2.mat, but that can vary. From there on the script extracts the path to only the base directory resulting in C:\Program Files\QGIS 3.22.5\ in my example case.

PS C:\> cscript qgis_install_path.vbs "{DEA3CAFB-7511-1014-A941-05998B569609}"

...
C:\Program Files\QGIS 3.22.5\

The script referenced above: (Save as qgis_install_path.vbs or however you want to name it)

if WScript.Arguments.Count = 0 then
    WScript.Echo "Missing input parameter!"
    Wscript.Quit 2
end if

Dim i : Set i = Nothing
Set i = Wscript.CreateObject("WindowsInstaller.Installer")

QGIS_PRODUCT_ID = WScript.Arguments(0)

Dim component, components, client, clients, path
Set components = i.Components : CheckError
For Each component In components
    Set clients = i.ComponentClients(component) : CheckError
    For Each client In Clients
        If client = QGIS_PRODUCT_ID Then
            path = i.ComponentPath(QGIS_PRODUCT_ID, component) : CheckError
            qgisIndex = InStr(path, "QGIS")
            seperatorIndex = InStr(qgisIndex, path, "\")
            message = Mid(path, 1, seperatorIndex)
            EndCheck
            Exit For
        End If
    Next
    Set clients = Nothing
Next
Set components = Nothing
Wscript.Echo "Did not find QGIS installation path!"

sub EndCheck
  Wscript.Echo message
  WScript.Quit
end sub

Sub CheckError
    Dim message, errRec
    If Err = 0 Then Exit Sub
    message = Err.Source & " " & Hex(Err) & ": " & Err.Description
    If Not installer Is Nothing Then
        Set errRec = installer.LastErrorRecord
        If Not errRec Is Nothing Then message = message & vbNewLine & errRec.FormatText
    End If
    Wscript.Echo message
    Wscript.Quit 2
End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.