Creating a temporary folder during installation in WiX

3.7k views Asked by At

In WiX, how to create a temporary hidden folder (Like SUPPORTDIR In IS)on the target machine,during installation and how to add the files to it, that are stored in the Binary table, and then subsequently delete it after Installation is completed in wix?

Grateful to any help.

Thanks.

1

There are 1 answers

1
Yan Sklyarenko On

This is a sample solution that extends the idea I referenced in my own comment.

To tell it short, you don't have to complicate the solution by adding extra files to the binary table, extract them in a custom action and remove afterwards. The built-in mechanism on DTF custom actions does it for you.

Let's say you have an XML file you'd like to read in the custom action to output some info to the installation log file. First of all, you should create a Custom Action project in VS (File -> New -> Project... and choose "C# Custom Action project" template).

Then, add an XML file as a content: right-click the project in the Solution Explorer, choose Add -> New Item... and select XML file. Let's call it data.xml. The contents might look like this:

<?xml version="1.0" encoding="utf-8" ?>
<settings>
  <setting name="a" value="one" />
  <setting name="b" value="two" />
</settings>

Next, add some code to read the values from this XML and write something to the installation log (remember, it's just a sample):

[CustomAction]
public static ActionResult ReadXml(Session session)
{
  var doc = XDocument.Load("data.xml");
  var settings = from setting in doc.Descendants("setting")
                 select new
                 {
                   Name = setting.Attribute("name").Value,
                   Value = setting.Attribute("value").Value
                 };

  foreach (var setting in settings)
  {
    session.Log(string.Format("{0} = {1}", setting.Name, setting.Value));
  }

  return ActionResult.Success;
}

Note that I reference the XML file as just data.xml - when custom action runs, it will find the file in the same directory it runs from.

Finally, add your custom action to the installation process:

<Binary Id="MainBinary" SourceFile="bin\ExtraFiles.CA.dll" />
<CustomAction Id="ReadXmlCA" BinaryKey="MainBinary" DllEntry="ReadXml" Execute="deferred" />

<InstallExecuteSequence>
  <Custom Action="ReadXmlCA" After="InstallFiles" />
</InstallExecuteSequence>

Now, build the MSI package and run the installation the following way:

msiexec -i SupportDir.msi /l*v install.log

When the package installation completes, open the install.log file and search for CustomActions.ReadXml. You'll see something like this:

SFXCA: Extracting custom action to temporary directory: C:\Windows\Installer\MSIAB8D.tmp-\
SFXCA: Binding to CLR version v4.0.30319
Calling custom action ExtraFiles!ExtraFiles.CustomActions.ReadXml
a = one
b = two

Obviously, custom action has done its job: it read the XML file contents and output proper info to the log file. One more thing to note: the temp directory path. Try to navigate to that directory - you'll find out that it is empty.

This means DTF took care about extracting files to the proper location to make it available for the custom action code, and cleaning up after the job is done.

Hope this overview helps understanding the way it works.