Search code examples
visual-studiowindows-10wixwindows-installer

How to use Wix Toolset's HarvestDirectory?


I want to automatically add a folder, C:\example\dir, to a Wix Toolset MSI installer.

I used the HarvestDirectory target like this:

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <?include "Config.wxi" ?>
    <Product Id="*" Name="programName" Language="1033" Version="1.0.0.0" Manufacturer="Me" UpgradeCode="2275bf6b-489e-49f3-a7fd-cfd96ed94d7b">
        <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" Platform="x64" />

        <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
        <MediaTemplate EmbedCab="yes" />

        <Feature Id="ProductFeature" Title="programName" Level="1">
            <ComponentGroupRef Id="BinDirRefId" />
        </Feature>

        <UIRef Id="WixUI_InstallDir" />

        <Property Id="WIXUI_INSTALLDIR" Value="INSTALLFOLDER" />
    </Product>

    <Fragment>
        <Directory Id="TARGETDIR" Name="SourceDir">
            <Directory Id="ProgramFiles64Folder">
                <Directory Id="INSTALLFOLDER" Name="programName">
                    <Directory Id="BinDirRefId" Name="bin">
                    </Directory>
                </Directory>
            </Directory>
        </Directory>
    </Fragment>

    <ItemGroup>
        <HarvestDirectory Include="C:\example\dir">
            <DirectoryRefId>BinDirRefId</DirectoryRefId>
        </HarvestDirectory>
    </ItemGroup>
</Wix>

Visual Studio tells me that ItemGroup isn't a valid child of <Wix>. How should I use this target exactly?


Solution

  • Like the vcxproj or csproj, these are helpers for MsBuild, the ItemGroup needs to be added to the wixproj too to simplify the build. Then you can use the same Directory id as the example.

    Same snippet adjusted to your attempt:

    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
      ...
      </PropertyGroup>
      <ItemGroup>
        <Compile Include="foo.wxs" />
        <HarvestDirectory Include="C:\example\dir">
          <DirectoryRefId>BinDirRefId</DirectoryRefId>
          ...
        </HarvestDirectory>
        <WixExtension Include="WixUIExtension" />
      </ItemGroup>
      <Import Project="$(WixTargetsPath)" />
    </Project>