From MSI to WiX, Part 15 - Installable Items - Ini files

The main page for the series is here.

 

Introduction

Today's topic is how to create and modify Ini files.

MSI story

Ini file contains configuration information that application needs during run-time.

MSI has two tables related to Ini files:  IniFile and RemoveIniFile.   IniFile table contains information about data which need to added to the Ini file on install or removed from the Ini file on uninstall.  RemoveIniFile table describes the data which need to be removed from the Ini file on install.

Two standard actions must be scheduled in the Execute sequence tables: WriteIniValues and RemoveIniValues.

Here is the list of columns of the IniFile table:

Column Description
IniFile The primary key for the table.
FileName

The localizable name of the .ini file in which to write the information.

DirProperty The name of the property which will be resolved to the full path of the folder containg the .ini file.  If this field is blank, the WindowsFolder property will be used to determine the path.
Section

The localizable .ini file section.

Key The localizable .ini file key within the section.
Value

The localizable value to be written.

Action The type of modification to be made.
Component_ External key into the first column of the Component table.

Here is the list of columns of the RemoveIniFile table:

Column Description
RemoveIniFile The primary key for the table.
FileName

The localizable name of the .ini file in which to delete the information.

DirProperty The name of the property which will be resolved to the full path of the folder containg the .ini file.  If this field is blank, the WindowsFolder property will be used to determine the path.
Section

The localizable .ini file section.

Key The localizable .ini file key within the section.
Value

The localizable value to be deleted. The value is required when Action is 4.

Action The type of modification to be made.
Component_ External key into the first column of the Component table.

Removing the last value from a section deletes that section. There is no other way to delete an entire section other than removing all its values.

What we can do with the Ini file 

These are the options we have when we add/delete value:

  • Create a new entry or update it if entry already exist.
  • Create a new entry if it does not exist.
  • Remove entry.
  • Create a new entry or append a tag if entry already exist.
  • Remove a tag from the entry.  If entry becomes empty, it will be removed.

Also, keep in mind that if you delete a last entry in the .ini file, file will be deleted.

How it translates to WiX?

To create/delete entries:

IniFile/Action RemoveIniFile/Action <IniFile> attribute Description
msidbIniFileActionAddLine (0)   Action="addLine" Create or update an .ini entry.
msidbIniFileActionCreateLine (1)   Action="createLine" Create an .ini entry if entry does not exist.
msidbIniFileActionAddTag (3)   Action="addTag" Creates a new entry or appends value to existing entry.
  msidbIniFileActionRemoveLine (2) Action="removeLine" Removes an entry.
  msidbIniFileActionRemoveTag (4) Action="removeTag" Removes a tag from an entry.  If entry's value becomes empty, entry is removed.

Examples

  • Create a new entry if it does not exist: 

     <IniFile Id="Ini1"

              Action="createLine"

              Directory="INSTALLLOCATION"

              Section="Test"

              Name="Minimal.ini"

              Key="TestKey"

              Value="TestValue" />

 Creates this:

[Test]

TestKey=TestValue

  • Add a tag to already existing entry:

     <IniFile Id="Ini1"

              Action="addTag"

              Directory="INSTALLLOCATION"

              Section="Test"

              Name="Minimal.ini"

              Key="TestKey"

              Value="TestValue2" />

Creates this:

[Test]

TestKey=TestValue,TestValue2

  • Remove a tag:

     <IniFile Id="Ini1"

              Action="removeTag"

              Directory="INSTALLLOCATION"

  Section="Test"

              Name="Minimal.ini"

              Key="TestKey"

              Value="TestValue" />

Creates this:

[Test]

TestKey=TestValue2

Here is the sample which installs ini file both in the installation folder and Windows folder.  Code for this sample is attached.

<?xml version="1.0" encoding="UTF-8"?>

<Wix xmlns="https://schemas.microsoft.com/wix/2003/01/wi">

  <?include ..\..\Common\CommonDefinitions.wxi ?>

  <?include Definitions.wxi ?>

  <Product Id="$(var.ProductCode)"

           Name="$(var.ProductName)"

           Language="$(var.Language)"

           Version="$(var.CurrentVersion)"

           Manufacturer="$(var.Manufacturer)"

           UpgradeCode="$(var.UpgradeCode)" >

    <Package Id="$(var.PackageCode)"

             Description="$(var.PackageDescription)"

             Comments="$(var.PackageComments)"

             InstallerVersion="200"

             Compressed="yes"

             Languages="$(var.Languages)" />

    <?include ..\..\Common\ARP.wxi ?>

    <Media Id="1" Cabinet="Product.cab" EmbedCab="yes" />

    <?include ..\..\Common\UpgradeSupport.wxi ?>

    <Directory Id="TARGETDIR" Name="SourceDir">

      <Directory Id="ProgramFilesFolder">

        <Directory Id="INSTALLLOCATION"

                   Name="Minimal"

                   LongName="MinimalInstallation">

          <Component Id="TestIni"

                     Guid="{DC752365-A598-4B76-AC60-C99BF34D539F}">

            <CreateFolder />

            <IniFile Id="Ini1"

                     Action="createLine"

                     Directory="INSTALLLOCATION"

                     Section="Test"

                     Name="Minimal.ini"

                     Key="TestKey"

                     Value="TestValue" />

            <IniFile Id="Ini2"

                     Action="createLine"

                     Directory="WindowsFolder"

                     Section="Test"

                     Name="Minimal.ini"

                     Key="TestKey"

                     Value="WindowsFolder TestValue" />

          </Component>

        </Directory>

      </Directory>

    </Directory>

    <Feature Id="MainFeature"

             Title="TestApp application"

             Level="1">

      <ComponentRef Id="TestIni" />

    </Feature>

  </Product>

</Wix>

 

Part15.zip