Inno Setup: Determining the data directory at install time

Inno Setup is a fantastic and free application for creating Windows setup programs. It takes so much of the hassle out of the process that it's almost fun.

I recently needed to extend Inno Setup with a custom code section: I needed to install the program's data to the Common App Data folder or the Local App Data folder, depending on a selection made during the installation process (install for all users, or just me). Here's the script I came up with:

; AppDataTest.iss
[Setup]
AppName=AppData Test
AppVerName=AppData Test 0.1
DefaultDirName={pf}\AppDataTest
DefaultGroupName=AppDataTest
OutputDir=Setup

[Files]
Source: AppDataTest.iss;  DestDir: {app}\;
Source: AppDataTest.iss;  DestDir: {code:DefAppDataFolder}\Test\;

[Tasks]
Name: common; Description: "&All users"; GroupDescription: "Install For:"; Flags: exclusive unchecked
Name: local;  Description: "Just &me"; GroupDescription: "Install For:"; Flags: exclusive

[Icons]
Name: {group}\Uninstall AppDataTest; Filename: {uninstallexe}

[Code]
function DefAppDataFolder(Param: String): String;
begin
  if IsTaskSelected('common') then
    Result := ExpandConstant('{commonappdata}')
  else
    Result := ExpandConstant('{localappdata}')
end;

I created a radio button for the [Tasks] page, asking whether the user wants to install the application for all users, or just herself.

Then in the [Code] section, I created a function to expand "DefAppDataFolder" to commonappdata or localappdata, depending on whether "common" or "local" is selected in in the [Tasks] section (IsTaskSelected('common')).

This value is then expanded in the [Files] section, at this line:

Source: AppDataTest.iss;  DestDir: {code:DefAppDataFolder}\Test\;

Here's the setup script (AppDataTest.iss), and the resulting setup program (setup.exe).

3 comments to Inno Setup: Determining the data directory at install time

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>