Recently I put together a Windows Installer package (MSI file) for our team to use internally.
The app I was installing writes to temporary storage, and the app stores a reference to the temporary storage folder in AppConfig.xml, which is included in the install manifest. AppConfig.xml contains a folder specifier like this:
<Options> <CacheFolder>c:\cache</CacheFolder> </Options>
The problem I faced was that I wanted to avoid using a hardcoded “c:\cache.” Not every user has a C drive or wants a cache folder at the root. And I didn’t want the user to have to configure this manually. The installer should default to a reasonable location.
Fortunately, WiX handles this sort of thing with aplomb. You can modify the contents of XML files at install time. Perfect!
Here’s how. In the WXS manifest, add an XML setValue action to modify XML contents.
<?define AppConfigFile = "AppConfig.xml" ?> <?define CacheFolder = "cache" ?> <DirectoryRef Id="InstallDir"> <Component Id="Config" Guid="<your-GUID-here>"> <File Id="Config" Source="$(var.AppConfigFile)" KeyPath="yes"/> <util:XmlFile Id="Cache" File=[#MyAppConfig]" Action="setValue" ElementPath="//Options/CacheFolder" Value="[TempFolder]$(var.CacheFolder)" /> </Component> </DirectoryRef>
Here’s what’s happening. You define your file component as normal. After you’ve specified the file information, use the util:XmlFile specifier. XmlFile File points at the actual file by using the # specifier, which resolves to the full install path of AppConfig.xml. Change the XML value of to “%temp%cache”, which resolves to the user’s temporary folder plus “cache,” e.g. “c:\users\pete\AppData\Local\Temp\cache” — just what we want.
To make this all work correctly, there are two more steps. First, ensure your WSX source references the utility extension where util:XmlFile is located.
<?xml version="1.0" encoding="UTF-8"?> <Wix xmlns = "http://schemas.microsoft.com/wix/2006/wi" xmlns:util = "http://schemas.microsoft.com/wix/UtilExtension">
And second, reference the extension when you compile and link:
candle source.wxs -ext WixUtilExtension.dll light source.wixobj -ext WixUtilExtension.dll
That’s it. Now the installation modifies the config file so your app automatically caches data to the user’s personalized temporary folder.