WiX: Setting the Install Directory from an Environment Variable

I pulled my hair out for hours trying to figure out how to get WiX to generate an installer that pulled the target install directory from an existing environment variable. This is easy to do if the variable is one of the standard windows file locations like “Program Files” or “App Data”. However, if the environment variable is a custom one, its not so straightforward.

To get it to work, I had to use a custom action to set the directory value. And if I wanted it to show up in the UI, I had to run the custom action in the InstallUISequence (not the InstallExecuteSequence).

Below is the code that I ended up using to get it all to work.

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

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <?include VariableDefinitions.wxi ?>

  <Product Id="86f82e0c-04e1-4c3d-9c8a-a295ee9cee0d"
           Name="$(var.ProductName) $(var.Version)"
           Language="1033"
           Version="$(var.Version)"
           Manufacturer="$(var.ProductManufacturer)"
           UpgradeCode="$(var.ProductUpgradeCode)">

    <Package Id="*"
             InstallerVersion="200"
             Platform="x86"
             Manufacturer="$(var.ProductManufacturer)"
             Description="$(var.PackageDescription)"
             Compressed="yes"
             Comments="$(var.PackageComments)" />

    <!-- The custom action to set the install directory from an environment var. -->
    <CustomAction
      Id="SetApplicationRootDirectory"
      Directory="APPLICATIONROOTDIRECTORY"
      Value="[%CustomFolderName]bin" />

    <!-- Media here -->

    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <!-- DefaultFolderName will be overridden by the environment var value. -->
        <Directory Id="APPLICATIONROOTDIRECTORY" Name="DefaultFolderName">
          <!-- Components here -->
        </Directory>
      </Directory>
    </Directory>

    <!-- Features here -->

    <InstallUISequence>
      <!-- Execute the custom action to set the install folder. -->
      <Custom Action="SetApplicationRootDirectory" After="CostFinalize" />
    </InstallUISequence>

  </Product>
</Wix>

I hope this post helps you avoid wasting the time I did trying to solve this issue.

-=Chris=-

This entry was posted in Wix and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments