天天看点

WIX配置-打包文件添加文件到安装包

    自从把开发环境从VS2010升到VS2012后,一直存在一个困扰的问题-制作安装包。开发时需要使用VS2012,打包软件时却使用VS2010。使用不方便,希望能够寻找一个在VS下能够使用,并且方便做持续集成的打包工具。先研究VS2012自带的InstallShield发现是一个坑,居然是一个有限制的第三方工具,高级版本还需要收费。后来在度娘的帮助下发现了WIX(Windows Install XML),经过初步研究能够满足需求。于是从下载Wix3.9开始配置之旅。

添加文件到安装包

对于一个安装包的制作其中一项最主要的哪些文件需要被包含和被组织在什么样的目录中。下面将依次介绍该配置过程。

第一步:定义目录结构

    安装程序时一般是把一些文件拷贝到磁盘上的一些目录中。为了提高WIX文件的可读性,推荐单独定义文件目录。使用<Directory>元素定义文件目录。在WIX文件中<Directory>可以被包含在Directory, DirectoryRef, Fragment, Module, Product中,其子集元素可以为Component、Directory、Merge、SymbolPath。下面是一个程序的安装目录的示例:

<Directory Id="TARGETDIR" Name="SourceDir"> 

 <Directory Id="ProgramFilesFolder"> 

<Directory Id="APPLICATIONROOTDIRECTORY" Name="MyApplication "/> 

</Directory> 

 </Directory>       

作为安装程序来讲,第一个安装目录的Id必须为"TARGETDIR"。

第二个目录Id为"ProgramFilesFolder"一般指向的是"c:\Program Files\"。

第三个目录Id为"APPLICATIONROOTDIRECTORY"或其他自定义全大写的字符串,在WIX文件中的其他地方将会被使用。Name为MyApplication,安装后目录就会为"c:\Program Files\MyApplication"。

第二步:添加文件到安装包

    文件目录定义好后文需要把必要的文件添加到安装包中。这儿主要是通过DirectoryRef、Component、File三个元素实现,以下文一个简单的示例:

<DirectoryRef Id="APPLICATIONROOTDIRECTORY">

<Component Id="WindowsFormsApplication.exe" Guid="4039b7c2-b7be-48fe-b082-231215c34412">

<File Id="WindowsFormsApplication.exe" Source="MySourceFiles\WindowsFormsApplication.exe" KeyPath="yes" Checksum="yes"/>

</Component>

<Directory Id="Files" Name="Files">

<Component Id="noimage1.jpg" Guid="4030b7c2-b7be-48fe-b282-231215cb8812">

<File Id="noimage1.jpg" Source="MySourceFiles\files\noimage1.jpg" KeyPath="yes"/>

</Component>

<Component Id="noimage.jpg" Guid="4030b7c2-b98e-48fe-b282-231215cb8412">

<File Id="noimage.jpg" Source="MySourceFiles\files\noimage.jpg" KeyPath="yes"/>

</Component>

</Directory>

</DirectoryRef>

DirectoryRef的Id为第一步中定义APPLICATIONROOTDIRECTORY;

Component中id必须唯一,必须包含唯一的guid,其下只能有一个File元素;

File的id需要唯一,Source可以为绝对路径或对于wxs的相对路径,必须包含KeyPath="yes",如果为文件为可执行文件还需要包含Checksum="yes";

第三步:把文件添加到安装队列中

    由于安装文件可能会比较多,一般可以把其组织到一个当独的ComponentGroup中,再把ComponentGroup添加到Feature中,示例如下:

<ComponentGroup Id="ProductComponents" Directory="APPLICATIONROOTDIRECTORY">

<ComponentRef Id="WindowsFormsApplication.exe"/>

<ComponentRef Id="noimage1.jpg"/>

<ComponentRef Id="noimage.jpg"/>

        </ComponentGroup>

        <Feature Id="ProductFeature" Title="SetupProject" Level="1">

            <ComponentGroupRef Id="ProductComponents" />

        </Feature>

    在安装程序时会根据Feature中的先后顺序就行安装。

完整的示例文件:

<?xml version="1.0" encoding="UTF-8"?>

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">

    <Product Id="4039b7c2-b7be-48fe-b082-231215cb8412" Name="SetupProject" Language="1033" Version="1.0.0.0" Manufacturer="man" UpgradeCode="4039b7c2-b7be-48fe-b082-231215cb8412">

        <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />

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

        <MediaTemplate />

        <Feature Id="ProductFeature" Title="SetupProject" Level="1">

            <ComponentGroupRef Id="ProductComponents" />

        </Feature>

    </Product>

    <Fragment>

        <Directory Id="TARGETDIR" Name="SourceDir">

            <Directory Id="ProgramFilesFolder">

                <Directory Id="APPLICATIONROOTDIRECTORY" Name="MyApplication " />

            </Directory>

        </Directory>

<DirectoryRef Id="APPLICATIONROOTDIRECTORY">

<Component Id="WindowsFormsApplication.exe" Guid="4039b7c2-b7be-48fe-b082-231215c34412">

<File Id="WindowsFormsApplication.exe" Source="MySourceFiles\WindowsFormsApplication.exe" KeyPath="yes" Checksum="yes"/>

</Component>

<Directory Id="Files" Name="Files">

<Component Id="noimage1.jpg" Guid="4030b7c2-b7be-48fe-b282-231215cb8812">

<File Id="noimage1.jpg" Source="MySourceFiles\files\noimage1.jpg" KeyPath="yes"/>

</Component>

<Component Id="noimage.jpg" Guid="4030b7c2-b98e-48fe-b282-231215cb8412">

<File Id="noimage.jpg" Source="MySourceFiles\files\noimage.jpg" KeyPath="yes"/>

</Component>

</Directory>

</DirectoryRef>

    </Fragment>

    <Fragment>

        <ComponentGroup Id="ProductComponents" Directory="APPLICATIONROOTDIRECTORY">

<ComponentRef Id="WindowsFormsApplication.exe"/>

<ComponentRef Id="noimage1.jpg"/>

<ComponentRef Id="noimage.jpg"/>

        </ComponentGroup>

    </Fragment>

</Wix>

转载于:https://www.cnblogs.com/pughua/p/4245329.html