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

被 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 />” 改写成 “<Media Id="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


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值