How to add all files from a folder to MSI Installer using WIX

This shows how to add all files from a folder without typing and defining each file to wxs source. HeatDirectory is a WiX utility enabling the harvesting of all files, and it will be added to BeforeBuild Target of Wix Project, and run before compiling.

Open WiX Project

Refer to an article on how to start a Wix Project. Open the Wix project in a text editor.

Define a HeatDirectory in a Target

HeatDirectory is a wrapper for Wix Harvester utility heat.exe, which can be added as a task in a BeforeBuild Target. This Target is processed before Compile, and will generate a file list wix source (here, onedir.wxs ).

<Target Name="BeforeBuild">
    <PropertyGroup>
           <DefineConstants>ONEDIRPATH=..\OneDir</DefineConstants>
    </PropertyGroup>
    <HeatDirectory Directory="..\OneDir" 
                   DirectoryRefId="INSTALLFOLDER" 
                   OutputFile="onedir.wxs" 
                   ComponentGroupName="OneDirComponentGroup" 
                   PreprocessorVariable="var.ONEDIRPATH" 
                   ToolPath="$(WixToolPath)" 
                   AutogenerateGuids="true" 
                   Transforms="OneDirTransform.xslt" 
                   SuppressRootDirectory="true" 
                   SuppressRegistry="true" />
</Target>

Transforms (here, OneDirTransform.XSLT) does not have to be defined if all files are to be a part of the package. If any files are to be filtered out, xslt can define the rule of file inclusion and exclusion. We can cover more about the transformations in a different article.

Include the output file to Compile list

The generated file list Wix (onedir.wxs in this example) is an intermediate source. The filename should be included in Compile list in the Wix project.

<ItemGroup>
  ....
  <Compile Include="outdir.wxs" /> 
</ItemGroup>

Add ComponentGroupRef Id to main source, ‘Product.wxs’

There is a “Feature” section of <Product>. ComponentGroupRef is to be added, a reference for the Component Group from a different fragment.

<Feature Id="..." ....>
  <...>
  <ComponentGroupRef Id="OneDirComponentGroup" />
</Feature>

Build the Project

In Visual Studio, at 1st run, a warning icon will be shown on the to-be-generated file as onedir.wxs was not generated yet.

At Visual Studio dev command prompt, MSBuild can be used to build, too.

msbuild simple.wixproj /t:Build /p:Configuration=Release

An intermediate onedir.wxs was generated as an above screenshot, and an MSI is created with all files from the folder.

Run:

More articles to read:

How to Exclude Files Using XSLT in Wix Installer

How to Create WiX Project in Visual Studio 2022

How to Create Launch Conditions in WiX Installer

How to Install Window Services Using WiX

How to Create Desktop and Start Menu Shortcuts in WiX Installer

Leave a Comment