How to customize the install directory dynamically in WiX?

This is one of those elusive topics whose answer is hard to find with googling around, but you will discover that it’s pretty easy, again if only you knew the magic words.

 

Define this in your Main.wxs:

<Property Id="_BrowseProperty" Value="INSTALLDIR" />
<Directory Id="ProgramFilesFolder" SourceName="program files">
  <Directory Id="INSTALLDIR" Name=".">
    <Directory Id="FOOBAR_DIR" Name="FOOBAR">
      <!-- All your components goes here…… -->
    </Directory >
  </Directory >
</Directory >

Define the Ok (pushbutton) and PathEdit controls in your Browse.wxs dialog for browsing directories to specify the install path as:

<Control Id="PathEdit" Type="PathEdit" X="84" Y="202" Width="261" Height="20" Property="_BrowseProperty" Indirect="yes" />
<Control Id="OK" Type="PushButton" X="304" Y="243" Width="56" Height="17" Default="yes" Text="[ButtonText_OK]">
  <Publish Event="SetTargetPath" Value="[_BrowseProperty]"><![CDATA[1]]></Publish>
  <Publish Event="EndDialog" Value="Return"><![CDATA[1]]></Publish>
</Control>

And finally wire up the events in the Next button in the Customize.wxs Dialog which spawns the Browse.wxs dialog:

<Control Id="Next" Type="PushButton" X="330" Y="308" Width="56" Height="17" Default="yes" Text="[ButtonText_Next]">
  <Publish Event="SetTargetPath" Value="INSTALLDIR"><![CDATA[1]]></Publish>
  <Publish Event="NewDialog" Value="NextDlg"><![CDATA[1]]></Publish>
  <Subscribe Event="SelectionNoItems" Attribute="Enabled" />
</Control>

So how did this really work?

So as we can see INSTALLDIR is currently setup as “C:\Program Files\” (note the parent of “INSTALLDIR” is “Program Files”) but defining the INSTALLDIR with a Name attribute = “.” simply ignores the root parent folder if it finds INSTALLDIR was assigned a different value which we do in Browse.wxs dialog and uses the newly assigned install path.

About

I love coffee!

Posted in Programming, Windows Installer XML Tagged with: ,
2 comments
Mike
Mike

Genius! 3 years after this post, and you just saved me hours of frustration. Thank you, thank you, thank you!