
Installing directories onto the target computer
Before we can install any files to the end user's system, we have to specify the directories where they'll go. These could be directories that already exist or new ones that we'll be creating. Either way, we'll be using the Directory
elements to form our folder structure. Our Directory
elements can be nested directly within our Product
element or separated into a Fragment
. The Fragment
approach has the advantage of being more modular, or in other words, keeps the concerns of making a directory structure separate from other chores such as installing files into those directories.
In this recipe, we will install some directories into the Program Files
folder. To keep things simple, we won't install any files yet. Since Windows Installer won't install empty directories, we'll have to put some kind of placeholder in for now. We can use the CreateFolder
element for this. Its job is simply to ensure the directory gets created even though it's empty.
Getting ready
To prepare for this recipe, create a new setup project and call it DirectoryInstaller
.
How to do it…
Define a Directory
element that targets the Program Files
folder and then nest subdirectories within it. The following steps show you how it is done:
- The default markup that we find in our
Product.wxs
file already contains the skeleton of a directory structure. ADirectory
with anID
ofProgramFilesFolder
targetsProgram Files
:<Fragment> <Directory Id="TARGETDIR" Name="SourceDir"> <Directory Id="ProgramFilesFolder"> <Directory Id="INSTALLFOLDER" Name="DirectoryInstaller" /> </Directory> </Directory> </Fragment>
Tip
Downloading the example code
You can download the example code files for all Packt Publishing books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
- Remove the directory that has an ID of
INSTALLFOLDER
, replacing it with the following where eachDirectory
element's ID is unique and the name is the friendly name of the folder. This will create three folders—Config
,Tools
, andDocumentation
—within a folder calledMy Software
that's within a folder calledMy Company
:<Directory Id="ProgramFilesFolder"> <Directory Id="MyCompanyFolder" Name="My Company"> <Directory Id="MySoftwareFolder" Name="My Software"> <Directory Id="ConfigFolder" Name="Config" /> <Directory Id="ToolsFolder" Name="Tools" /> <Directory Id="DocFolder" Name="Documentation" /> </Directory> </Directory> </Directory>
- To ensure our empty directories are created, update the
ComponentGroup
that's at the bottom of the defaultProduct.wxs
file with the followingComponent
elements. Each contains theCreateFolder
tag that will allow its directory to be installed:<Fragment> <ComponentGroup Id="ProductComponents"> <Component Id="cmpCreateConfigFolder" Guid="{21AC0239-87F9-4D8B-9F73-71665C491150}" Directory="ConfigFolder"> <CreateFolder /> </Component> <Component Id="cmpCreateToolsFolder" Guid="{7B75B591-58B7-41F4-A511-E221E243371C}" Directory="ToolsFolder"> <CreateFolder /> </Component> <Component Id="cmpCreateDocFolder" Guid="{B0BA1D63-110C-4169-9094-64F4103234E8}" Directory="DocFolder"> <CreateFolder /> </Component> </ComponentGroup> </Fragment>
How it works…
The first thing to notice is that the top-level Directory
element has the ID TARGETDIR
and the name of SourceDir
:
<Directory Id="TARGETDIR" Name="SourceDir">
This is how all directory structures should begin and is required by Windows Installer. TARGETDIR
identifies the hard drive where our installation will go and, by default, is set to the largest drive on the system. That's usually the C:
drive but even if it isn't, the child Directory
element with the ID ProgramFilesFolder
leaves no doubt where the files should go. From there, we can add subdirectories of our own. If you'd like to begin your directory structure under a different existing folder, see the following link for information about other built-in Directory
elements: http://msdn.microsoft.com/en-us/library/aa370905(v=vs.85).aspx#system_folder_properties. Note that predefined directories such as ProgramFilesFolder
don't get a Name
attribute.
For new directories that you create, the ID can be anything as long as it follows a few rules:
- It must be unique among other
Directory
elements - It should contain only letters, numbers, underscores, and periods
- It must begin with either a letter or an underscore
Windows Installer uses this identifier internally, but the end user will never see it. The Name
attribute, on the other hand, sets the visible name of the folder after it's installed. After installation, we can see that the directory structure was added to the computer under Program Files
:
