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

  1. Pawan Shukla
    Posted April 16, 2011 at 2:22 am | Permalink

    Hi Chris,

    Thanks! this is helpful

    I have one question here, if i have to set install directory conditionally then how will i be able to do that.

    Thanks
    Pawan Shukla

  2. Posted May 9, 2011 at 7:37 am | Permalink

    I only used WiX for a few days, and haven’t touched it since, so I am probably not the best person to answer the question. However, I googled it and found a few links that may help. Here they are:

    Hope this helps.

    -Chris

Post a Comment

Your email is never published nor shared. Required fields are marked *

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*
*