RichardS
RichardS

Reputation: 23

Read installer version from an .ini file stored in the same directory in Inno Setup

I want to distribute the compiled installer with a separate INI file. The setup will load configurations (e.g. a version) from an INI file supplied to that particular client.

I have a ver.ini file in the same directory as the Setup.exe. It contains-

[Version]
Ver=0.0.11

I want to use it like-

#define AppVer ReadIni("ver.ini", "Version", "Ver", "unknown")

The Problem: Inno Setup is showing AppVer as unknown, rather than 0.0.11.

If I use a hard-coded file path for the ver.ini file, it works:

#define AppVer ReadIni("C:\Users\Admin\Desktop\ver.ini", "Version", "Ver", "unknown")

Since I'm distributing the Setup to my customers, I cannot use a hard-coded file path.

Any help is appreciated!

Upvotes: 2

Views: 316

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202118

You cannot use a preprocessor. The preprocessor is executed on compile time. So even if you fix the path problem (e.g. using SourcePath preprocessor variable), it won't help, as you will be reading INI file on the machine that compiles your installer. Not on the machine, where the installer is executed.


You have to use Pascal Scripting and a scripted constant. Use GetIniString function to read the INI file and src constant to refer to the source directory.

[Setup]
AppVersion={code:GetAppVersion}
[Code]

function GetAppVersion(Param: string): string;
var
  IniPath: string;
begin
  IniPath := ExpandConstant('{src}\ver.ini');
  Result := GetIniString('Version', 'Ver', 'Unknown', IniPath);
end;

You may want to abort the installation in case the version cannot be found:

function GetAppVersion(Param: string): string;
var
  IniPath: string;
begin
  IniPath := ExpandConstant('{src}\ver.ini');
  Result := GetIniString('Version', 'Ver', '', IniPath);
  if Result = '' then RaiseException('Cannot find version');
end;

Upvotes: 1

Related Questions