How To Programmatically Retrieve The Version of Your Executable At Build Time

Written by Horatiu Vladasel · July 21st, 2021

#TIPS #MSI #BUILD

There are scenarios where you need to change a file version programmatically with each launch. To achieve that, we will set an installer property that changes with each release and then uses its value within your installer.

For example, when you retrieve the file version of the main executable for your application and then use that value for the install location of your application.

In this article, we will show you how to automatically retrieve the version of an executable using Advanced Installer - as well as an alternative solution using Pre Build Events.

How To Retrieve the Version of an EXE File by Using Advanced Installer’s GUI

To retrieve a previous or current version of an executable using Advanced Installer's GUI, follow these steps:

  1. Go to the Install Parameter page.
  2. Add a new property.
  3. From the “Edit Property” window, set the property's value to be automatically retrieved from the executable file.

How to Build your Pre-Build Event Script

Although Advanced Installer's solution makes it easy -- you can also fetch the version of your executables using Pre-Build Events.

Let’s imagine we need to retrieve the version of "RepackagerCLI.exe" and "updater.exe" executables (which come with every version of Advanced Installer).

NoteYou can learn more about Advanced Installer's Repackager and what it can offer you here.

The two previous executables can be found in Advanced Installer's installation folder, more specifically at the following path:

C:\Program Files (x86)\Caphyon\Advanced Installer 17.7\bin\x86

Now, let's write the script that will be implemented as a “Pre-Build Event”. Here is the one we used for this example:

# Script that can be used as a pre-build event in order to get the "Version" attribute of an EXE and store it in a property. We can further use this property to write the version value in the registries.
# As a prerequisite, please create the properties in your project with empty values (in order to create a property with an empty value, simply add a comment to it).
Add-Type -AssemblyName PresentationFramework
$AICom = "C:\Program Files (x86)\Caphyon\Advanced Installer 17.7\bin\x86\AdvancedInstaller.com"
$projectPath = "C:\Users\HoratiuVladasel\Desktop\Retrieve file version from multiple EXEs\Retrieve file version from multiple EXEs.aip"
$repackagerPath = "C:\Program Files (x86)\Caphyon\Advanced Installer 17.7\bin\x86\RepackagerCLI.exe"
$updaterPath = "C:\Program Files (x86)\Caphyon\Advanced Installer 17.7\bin\x86\updater.exe"

# get the version from the Repackager and store it in a variable
# the below cmdlet will return a string, which will look something like this: 1, 0, 0, 0. We will further use the "Replace()" method to replace the "," with "."
$repackagerVer = (get-item -path $repackagerPath).VersionInfo.FileVersion.Replace(',','.')
[System.Windows.MessageBox]::Show($repackagerVer)
$updaterVer = (get-item -path $updaterPath).VersionInfo.FileVersion.Replace(',','.')
[System.Windows.MessageBox]::Show($updaterVer)

# now that we have got the version of the EXEs, we will further set the properties that we created in our project.
Start-Process -FilePath $AICom -ArgumentList "/edit `"$projectPath`" /SetProperty REPACKAGER_VERSION=`"$repackagerVer`""
Start-Sleep -Seconds 2
Start-Process -FilePath $AICom -ArgumentList "/edit `"$projectPath`" /SetProperty UPDATER_VERSION=`"$updaterVer`""
Start-Sleep -Seconds 2
Start-Process -FilePath $AICom -ArgumentList "/save"

As you can see, this is what script does:

  1. It retrieves the version of the two executables (RepackagerCLI.exe and updater.exe) and stores the retrieved values in two variables (repackagerVer and updaterVer)
  2. It sets the value of two properties (REPACKAGER_VERSION and UPDATER_VERSION) to the corresponding variable using the /SetProperty command.

Now that we have our script and we know what it is meant for, we can proceed to create the Pre-Build event.

How to add the “Pre-Build” event

Once we have the script ready, all we need to do is implement it within the Advanced Installer project file.

To do this, follow these steps:

1. In the "Install Parameters" page, create one property for each of your executables and use a "blank value" to initialize the properties created.

Create property

ImportantIf the “Value” field of a property is left blank, then you need to fill the “Comment” field.

2. In the "Builds" page, go to “Build Events” and create a new “Pre-Build event” ("Add" --> "Pre-Build Event" --> "New...").

After creating a Pre-Build event, the following dialog will be prompted.

Edit event

How does the Pre-Build event work?

Now that everything is set up, you might wonder how the Pre-Build event update is executed. Here's a step-by-step flow of how the Pre-Build event works.

1. Build the project - the pre-build event will get executed.

2. Close the project.

3. Re-open the project - at this point, the properties added in the “Install Parameter” page are updated and populated accordingly.

Properties after reopen

4. Re-build the project – here, the corresponding properties are also added to the MSI database.

Properties after rebuild

NoteYou can check our dedicated page to learn more about pre-built events and how you can define them depending on your needs.

Conclusion

Now you have two simple options to follow if you need to dynamically retrieve the version of one or more executables at build time:

  1. By using Advanced Installer's GUI,
  2. By using the Pre-build Events.

These two options are very handy, especially if you wish to automate your installer build process.

We hope you found this quick tutorial useful.

What's your preferred way to retrieve previous or current versions of executables? Answer in the comments!

Subscribe to Our Newsletter

Sign up for free and be the first to receive the latest news, videos, exclusive How-Tos, and guides from Advanced Installer.

Comments: