WIX: Creating an MSI to deploy multiple files

In this example we are going to create a simple MSI that deploys a two files.  This is not that much different than one file, but there is an implementation decision you have to make here that is important to understand.

Prequisites

Step 1 – Create a WIX project in Visual Studio

Creating a new project is a very simple task once you have done it a few times. However, I try to make my walk-thrus newbie proof, so even some one who has never done this feels comfortable. So if you need help with this step, use my instructions below. If you don’t, skip them.

  1. Open Visual Studio if it is not already open.
  2. Got to File | New | Project.
  3. You should have an option under Installed Templates for Windows Install XML. Select it.
  4. Now you should see the option for Setup Project. Select it.
  5. Enter a Name for the project. I called the project I made for this walk-thru MultiFileProject.
  6. Change the directory to store the project if you want. It doesn’t matter what directory you choose, but it is nice to keep your learning projects organized.
  7. Click OK.

Your project should now be created. In solution explorer you should now have a solution, a project, a reference and the Product.wxs. See the image below.

Ok, I hope that was easy for you. Lets move on.

Step 2 – Add two files to the project

These steps are preformed in Visual Studio on the project you just created in the above step.

  1. Right-click on the project name, MultiFileProject, and from the drop down, choose Add | New Item.
  2. Select Text File.
  3. Name it whatever you want. I used the default value, TextFile1.txt for this example.
  4. Click Add.
  5. Repeat the steps above to create a second file. My second file is TextFile2.txt.

Your files are now added to the Visual Studio project. However, they are not automatically added into the Product.wxs as a file to be installed. This is done manually in the next step.

Step 3 – Take a moment to learn

We are going to change the Product.wxs file to include the file we just created.

The Product.wxs file has the following XML text in it. Take a moment to look at the XML nodes and their elements so are familiar with the syntax it is using.

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
	<Product Id="9a19f6ed-a7b2-4cfc-a429-7069066fbf2a" Name="MultiFileProject" Language="1033" Version="1.0.0.0" Manufacturer="MultiFileProject" UpgradeCode="c1bdcc4d-b249-4b36-97fd-32609a60f8b4">
		<Package InstallerVersion="200" Compressed="yes" />

		<Media Id="1" Cabinet="media1.cab" EmbedCab="yes" />

		<Directory Id="TARGETDIR" Name="SourceDir">
			<Directory Id="ProgramFilesFolder">
				<Directory Id="INSTALLLOCATION" Name="MultiFileProject">
					<!-- TODO: Remove the comments around this Component element and the ComponentRef below in order to add resources to this installer. -->
					<!-- <Component Id="ProductComponent" Guid="6263db4e-4c67-4adc-9ef1-e1caef798331"> -->
						<!-- TODO: Insert files, registry keys, and other resources here. -->
					<!-- </Component> -->
				</Directory>
			</Directory>
		</Directory>

		<Feature Id="ProductFeature" Title="MultiFileProject" Level="1">
			<!-- TODO: Remove the comments around this ComponentRef element and the Component above in order to add resources to this installer. -->
			<!-- <ComponentRef Id="ProductComponent" /> -->

			<!-- Note: The following ComponentGroupRef is required to pull in generated authoring from project references. -->
			<ComponentGroupRef Id="Product.Generated" />
		</Feature>
	</Product>
</Wix>

The text of the Xml gives us hints as to what we are supposed to do in its comments. Take a moment and read the comments.

Step 4 – Configure the WIX Xml file to deploy that file

Lets start editing that XML file.

Note: I am going to use the term “node’ to indicate and XML section. So and everything it contains is a node. If a node is inside it, I might call it a subnode.

  1. Uncomment the Component node the Directory nodes.
  2. Remove the two TODO: comments.
  3. Uncomment the ComponentRef node that is inside the Feature node.
  4. Remove the TODO: comment and the Note comment.
  5. Inside the Component node, at two File nodes for each of your two files as follows:
    <File Id='TextFile1.txt_id' Name='TextFile1.txt' Source='TextFile1.txt' KeyPath='yes' />
    <File Id='TextFile2.txt_id' Name='TextFile2.txt' Source='TextFile2.txt' />
    

    Notice that the second line is not marked with the KeyPath attribute. Only one file node (or maybe a registry node) can set KeyPath.

Ok, you are done.  Yes, that was all there is too it.   Feel free to add as many files as you want.

You XML syntax should now look as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Product Id="9a19f6ed-a7b2-4cfc-a429-7069066fbf2a" Name="MultiFileProject" Language="1033" Version="1.0.0.0" Manufacturer="MultiFileProject" UpgradeCode="c1bdcc4d-b249-4b36-97fd-32609a60f8b4">
    <Package InstallerVersion="200" Compressed="yes" />

    <Media Id="1" Cabinet="media1.cab" EmbedCab="yes" />

    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLLOCATION" Name="MultiFileProject">
          <Component Id="ProductComponent" Guid="6263db4e-4c67-4adc-9ef1-e1caef798331">
            <File Id='TextFile1.txt_id' Name='TextFile1.txt' Source='TextFile1.txt' KeyPath='yes' />
            <File Id='TextFile2.txt_id' Name='TextFile2.txt' Source='TextFile2.txt' />
          </Component>
        </Directory>
      </Directory>
    </Directory>

    <Feature Id="ProductFeature" Title="MultiFileProject" Level="1">
      <ComponentRef Id="ProductComponent" />
      <ComponentGroupRef Id="Product.Generated" />
    </Feature>
  </Product>
</Wix>

Ok, you are ready to build.

Step 4 – Build the project

Well, there really isn’t much to debug, so we are only going to build a release version here.

  1. In the Visual Studio 2010 tool bar, there should be a drop down box that either says Debug or Release. Change it to Release if it is not already at Release.
  2. Select Build | Build Solution or use the shortcut key.

You should now have an MSI built in the project’s bin\release directory.

Step 5 – Test the MSI

Let’s go get the MSI and test it.

  1. Right-click on the project, MultiFileProject, and choose, Open Folder in Windows Explorer.
  2. In Explorer, navigate into the bin\release directory.
  3. You will see two files:
    MultiFileProject.msi – This is the MSI and is all you need.
    MultiFileProject.wixpdb – This is a file for debugging only. You may never use it unless you need to debug.
  4. You may or may not want to test the MSI on you development box. If you do, just double-click the MSI. Otherwise, copy it to a test box and run the MSI there.
  5. Verify that the file installed.
    Note: If on a 64 bit system, it will by default install as an x86 app, so look in c:\program files (x86)\ for a folder called MultiFileProject.
  6. Check Add / Remove Programs to make sure the install shows up there and that the uninstall works.

Congratulations. You just use WIX to create an MSI that deploys multiple files.

Leave a Reply

How to post code in comments?