Tuesday, December 18, 2007

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.

 

 

No comments: