Windows Installer XML (WiX) 使用记录 (一)

来源:互联网 发布:非农数据图 编辑:程序博客网 时间:2024/05/16 01:13

被 Wix (Windows Installer XML) 折腾了很长的一段时间。为了确保以后不要被同样的问题折腾,决定把知识的都记录下来。

 

最简单的安装包。

1. 创建一个 Blank Solution,命名为 WixSample

2. 添加一个 C# 的 Console Application,命名为 HelloWorld,然后就是HelloWorld 的标准写法了。

using System;namespace HelloWorld{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("Hello World!");        }    }}

3. 假设这个Console Application project 编译出来的 HelloWorld.exe 就是我们需要安装的程序。</span>

4. 制作安装包:

        a.      创建一个 Wix 的 SetupProject.

                Create Wix setup project

        b.     一个 Product.wxs 会被自动创建出来。

<?xml version="1.0" encoding="UTF-8"?><Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"><Product Id="*" Name="HelloWorldInstaller" Language="1033" Version="1.0.0.0" Manufacturer="" UpgradeCode="7bfca941-7a3a-4987-aaee-cba952487eff"><Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" /><MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." /><MediaTemplate /><Feature Id="ProductFeature" Title="HelloWorldInstaller" Level="1"><ComponentGroupRef Id="ProductComponents" /></Feature></Product><Fragment><Directory Id="TARGETDIR" Name="SourceDir"><Directory Id="ProgramFilesFolder"><Directory Id="INSTALLFOLDER" Name="HelloWorldInstaller" /></Directory></Directory></Fragment><Fragment><ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER"><!-- TODO: Remove the comments around this Component element and the ComponentRef below in order to add resources to this installer. --><!-- <Component Id="ProductComponent"> --><!-- TODO: Insert files, registry keys, and other resources here. --><!-- </Component> --></ComponentGroup></Fragment></Wix>

                i.     如果尝试编译一下,是会有 error 的:

                “TheProduct/@Manufacturer attribute's value cannot be an empty string. If a valueis not required, simply remove the entire attribute.”

                有两个选择,要么把 Manufacturer="" 删掉;要么指定一下 Manufacturer,例如Manufacturer="The company name”

 

                ii.     再编译一下,没有 error 了,但是有warning:

                ICE71:The Media table has no entries.

                需要把 “<MediaTemplate />” 改写成 “<MediaId="1" Cabinet="media1.cab" EmbedCab="yes" />”

                老实说,这个具体什么含义我也不是很清楚,反正这样会压缩成一个单独的.msi 文件。当然,也可以选择压缩成多个.cab 文件。

                如果想知道更多,可以参考文档:

                http://wixtoolset.org/documentation/manual/v3/xsd/wix/media.html

 

                iii.     继续编译,遇到另外一个 warning:

                warningLGHT1079: The cabinet 'media1.cab' does not contain any files.  If this installation contains no files, thiswarning can likely be safelyignored. Otherwise,         please add files to the cabinet or remove it.

                原因为 .msi文件里面没有任何东西。从现在开始,我们需要真正地编写安装包了。

 

        c.      添加安装文件。

                        i.     添加输出项目。把 HelloWorldproject 添加到HelloWorldInstaller 的 Reference 里面

                Add HelloWorld reference

                Project name

                ii.     在名为 “ProductComponents”的 ComponentGroup 里面添加如下代码:

      <ComponentId="HelloWorld.exe"Guid="42F0F0ED-CC76-474E-95DD-71B780BDF4D2">        <File Id="HelloWorld.exe"Source="$(var.HelloWorld.TargetDir)HelloWorld.exe" />      </Component>

                        这堆代码的意思是:          

                        1.      创建一个id为HelloWorld.exe 的组件(Component),它的唯一标识符(Guid)为42F0F0ED-CC76-474E-95DD-71B780BDF4D2

                        2.      在这个组件里面包含了一个文件(File),它的id为HelloWorld.exe。也许有人会问那样Id不是冲突了么?

                                需要注意的是Component 和 File 是 父子级的关系,他们不再同一个作用域里面,所以不会冲突。但是要是在同一个Component 里面声明两个 id 为        HelloWorld.exe 的File,就会冲突了。

                        3.      代码 Source="$(var.HelloWorld.TargetDir)HelloWorld.exe" 是关键所在,它声明了在哪里可以找到这个HellowWorld.exe。

                        当在reference 里面添加 HelloWorld 项目的时候, 会自动生成变量 “var.HelloWorld.TargetDir” 。它的值是HelloWorld 项目的输出路径,通常要么是HelloWorld\bin\debug,或者HelloWorld\bin\Release。通过这个变量,就很容易找到需要安装的文件了。


                 iii.     再编译一下,无 error 无warning,大功告成。打开目录bin\debug(或者 bin\release) 会见到文件 HelloWorldInstaller.msi


        d.     运行安装包

                i.     双击安装包,会见到一个安装进度界面一闪而过。接着就没有然后了。。。

                ii.     在控制面板的卸载程序里面,我们可以看到  HelloWorldInstaller 已经装上了。 目录C:\Program Files (x86)\HelloWorldInstaller 里面也有我们想安装的        HelloWorld.exe。实际上已经安装成功了。

                iii.     在控制面板里面可以把 HelloWorldInstaller 彻底卸掉,完全不会留下痕迹。


        e.     OK,最简单的安装基本完成了,下一步是界面的问题。


完整源代码在 Github

https://github.com/IGabriel/WixSample/tree/A-Simplest-installer


0 0
原创粉丝点击