Search code examples
pathwixinstallationcustom-action

Setting install path based on machine name with WiX


I'm surprised I haven't been able to find a simple answer for this, since it seems like it would be a common thing to do.

I'm trying to set the install path based on the machine name of the server it is being installed on, as they have different folder structures.

I'm assuming I need some kind of custom action, but being brand new to WiX, I'm having a hard time figuring out where to start.

I suppose I can just make separate installers for each server (since it's only 3), but it would probably be better practice to just have one that's reusable.

A point in the right direction would be greatly appreciated! Thanks.


Solution

  • Suppose you have the following directory structure in your WiX authoring:

    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="INSTALLLOCATION" Name="nametochange">
        <!-- some component -->
        <Directory Id="BinDir" Name="bin">
           <!-- another component -->
        </Directory>
      </Directory>
    </Directory>
    

    The INSTALLLOCATION folder name should differ based on the machine name. You can author a SetDirectory element to set the directory path to the desired value (it is a hard code below, just to show the idea):

    <SetDirectory Id="INSTALLLOCATION" Value="C:\inetpub\wwwroot\[%COMPUTERNAME]" />
    

    The SetDirectory element schedules the underlying custom action properly. Note the way environment variable is referenced in WiX: [%COMPUTERNAME]. If you target Windows Installer 4.0+, you can rely on the special property ComputerName.

    Update: depending on the exact requirements, you can go further and experiment:

    <SetDirectory Id="INSTALLLOCATION" Value="C:\inetpub\wwwroot\FolderA">ComputerName = Server1</SetDirectory>
    <SetDirectory Id="INSTALLLOCATION" Value="C:\inetpub\wwwroot\FolderB">ComputerName = Server2</SetDirectory>
    <SetDirectory Id="INSTALLLOCATION" Value="C:\inetpub\wwwroot\FolderC">ComputerName = Server3</SetDirectory>
    

    etc.