Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps

Inno Setup Script to Change a C# Forms Configuration

5.00/5 (5 votes)
25 Feb 2017CPOL1 min read 15.4K   225  
Create an Installer which allows to change the Forms application settings in a custom Inno Setup page

Introduction

I needed an installer which could change the settings of a Windows Forms application during install. Inno Setup allows to program custom setup pages in the built in Pascal like scripting language.

Using the Code

Prerequisites

  • Inno Setup 5.5.2
  • Visual Studio 2013

The source code consists of a simple C# Windows Forms application and an Inno Setup script.

Image 1

;
;  Inno Setup 5.5 script to create an installer with a custom setup page to change a .config file.
;  see: Help - Pascal Scripting: Support Functions Reference
;
#define MyAppName "My Program"
#define MyAppVersion "1.0"
#define MyAppPublisher "My Company, Inc."
#define MyAppURL "http://www.google.com/"
#define MyAppExeName "SetupTest.exe"

[Setup]
AppId={{3FBF60B1-E3BD-410E-81B6-E41EA8BB9DAE}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={pf64}\{#MyAppName}
DefaultGroupName={#MyAppName}
OutputBaseFilename=setup
Compression=lzma
SolidCompression=yes

[Languages]
Name: "english";    MessagesFile: "compiler:Default.isl"

[Types]
Name: "normaltype"; Description: "Normal setup"
Name: "fulltype";   Description: "Full setup"
Name: "custom";     Description: "Custom installation"; Flags: iscustom

[Components]
Name: "normal";     Description: "Normal setup";  Types: normaltype custom
Name: "full";       Description: "Full setup";    Types: fulltype custom

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; 
GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked

[Files]
Source: "SetupTest\bin\Debug\SetupTest.exe";            
DestDir: "{app}"; Flags: ignoreversion
Source: "SetupTest\bin\Debug\SetupTest.exe.config";     
DestDir: "{app}"; Flags: ignoreversion

[Icons]
Name: "{group}\{#MyAppName}";         
Filename: "{app}\{#MyAppExeName}"
Name: "{commondesktop}\{#MyAppName}"; 
Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon

[Run]
Filename: "{app}\{#MyAppExeName}";    
Description: "{cm:LaunchProgram,
{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent

[Code]
var
  CustomPage: TInputQueryWizardPage;

///////////////////////// User clicked next button in setup.
function NextButtonClick(CurPage: Integer): Boolean;
var
  str: string;
  strFilename: string;

begin
  Result := true;

  if CurPage = wpSelectComponents then
   begin
      // if IsComponentSelected('full') then
      if (CustomPage = nil) then
      begin
        // Set Custom Page initial values
        CustomPage := CreateInputQueryPage(wpSelectComponents, 
        'Personal Information', 'Who are you?', 
        'Please specify your name, then click Next.');
        CustomPage.Add('Form title:', False);
        CustomPage.Add('Name:', False);
        CustomPage.Values[0] := 'My Form';
        CustomPage.Values[1] := 'John Doe';
      end;
   end;

  if CurPage = wpFinished then
   begin
      strFilename := ExpandConstant('{app}\SetupTest.exe.config');

      if FileExists(strFilename) then
      begin
        // Replace the values in the .config file and save it
        LoadStringFromFile(strFilename, str);
        StringChangeEx(str, 'My Form', CustomPage.Values[0], True);
        StringChangeEx(str, 'John Doe', CustomPage.Values[1], True);
        SaveStringToFile(strFilename, str, False);
      end;
   end;
end;

The C# application has two string settings defined (in Project - Properties - Settings),

'My Form' and 'John Doe'.
The settings are saved in the .config file.

The only thing this application does is display these settings in the Form title and a text field.

After building the C# application, the Inno Setup script can be run to build and test the installer.
Of course, it is also possible to only compile the script and execute the created installer in the Output folder later.

The installer will display the usual pages, and will also display a custom page where the Form title and user name can be changed:

Image 2

Image 3

This custom page is based on the TInputQueryWizardPage type.
After pressing the last "Finish" button, the .config file will be updated with the new settings.

When the application runs, it will display the changed settings:

Image 4

Points of Interest

For other custom page types, see the Inno Setup Help topic 'Pascal Scripting: Support Functions Reference'.

You can uninstall the application from Control Panel or with the 'unins000.exe' in the install folder.

History

  • Version 1.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)