Wix 软件打包(一)

简单的wix安装打包

  对winform或者wpf打包visual studio有自己的插件VS Installer,但是使用时遇到一些问题,如桌面快捷方式指向的文件不是运行程序,安装运行环境时每个环境都需要询问无法跳过等,在网上看到Wix(Windows Intstall XML)打包的方式。Wix是一种使用xml脚本配置部署的打包方式

1、首先在官网(https://wixtoolset.org/)下载WiX Toolset build tools 进行安装

点击Install安装,我这里是已经安装完成
在这里插入图片描述
2、Visual Studio 安装Wix Toolset Extension扩展功能

在这里插入图片描述
3、创建winform客户端MyApplication与打包项目 Setup for WIX 取名WxiSetup

在这里插入图片描述

WxiSetup 添加MyApplication的项目引用,在Product.wxs配置安装。

4、提示:代码说明

<Product Id="*" Name="WxiSetup" Language="2052" Version="1.0.0.0" Manufacturer="*******" UpgradeCode="abaddda2-473c-4b8c-8d7a-7b6fad502cd4">
      <!-- 配置节点 -->
      <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
	  <MediaTemplate />
	  <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />
	  <Feature Id="ProductFeature" Title="WxiSetup" Level="1">
      </Feature >
</Product>

Product 属性说明

Name 为安装包名称,注册表也是保存当前配置
Language 为区域性标志符(LCID)常见参数 1033 表示英语(美国),2052表示简体中文
Version 为安装包版本号
Manufacturer 为生产厂商
UpgradeCode 升级编码,随机GUID

节点说明

  • Package 记录了记录一些安装包的信息
      InstallerVersion: 安装此安装包需要的最小Windows Installer版本,用Windows Installer的主要版本乘以100 加上Window Installer的次要版本。 比如 “200” 代表的是Windows Installer2.0,而405代表的是Windows Installer4.5.
      Compressed: 这个为Yes 表示在源文件中含有压缩文件,对于Merge Module这个属性不必设置。反正为 NoType.
      InstallPrivileges: 字面意思为安装优先级,有limited 和 elevated两种 后者是默认值,
      InstallScope: 值为枚举类型,字面意思为安装范围,值必须是perMachine 或者 perUser. 看文档不知道确切的用处,先放过。

  • MajorUpgrade 这个元素支持防止降级
       AllowDowngrades:即回到低版本,如果设置为No(默认值) 会被阻止,这个时候DowngradeErrorMessage 属性必须设置,以给出提示。
      DowngradeErrorMessage:当你安装一个低版本的安装包时会给出的提示。

  • Feature 一个特性表,特性是可安装的最小单元。 子元素中的ComponentGroupRef 是和 ComponentGroup对应的。前者相当于一个安装目录,后者记录了安装文件的具体位置。
      Id: 唯一标识。
      Title: 就是个短的说明。
      Level: 安装的等级,值为0 会使这个特性无效,默认值为1
      Absent: 这个属性定义User是否有权在用户接口中去选择使某个特性不安装(absent),值为allow或者disallow之一

  • Fragment 元素是在wix中创建一个安装数据库的基础块(msi文件就是个数据库)。它的子元素中含有*Ref的元素必须有对应的单元,比如在Fragment中含有两个Component元素,那么 你必须在Feature中用ComponentRef 与Component对应. 默认生成的文档中含有两个Fragment块。 一个包含的是Directory,一个包含的是ComponentGroup ,前者指的是安装目录,后者顾名思义就是Component的一个集合。

<Feature Id="ProductFeature" Title="WxiSetup" Level="1">
	<ComponentGroupRef Id="ProductComponents" />
</Feature >

<Fragment>
	<Directory Id="TARGETDIR" Name="SourceDir">
		<Directory Id="ProgramFilesFolder" Name="PFiles">
			<Directory Id="INSTALLFOLDER" Name="WxiSetup" />
		</Directory>
	</Directory>
</Fragment>

<Fragment>
	<ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
		<Component Id="ProductComponent">
			<!--<File Id="myapplication.exe" Source="$(var.MyApplication.TargetPath)" />-->
				<File Id="myapplication.exe" Source="$(var.MyApplication.TargetDir)MyApplication.exe" />
				<File Id="myapplication.exe.config" Source="$(var.MyApplication.TargetDir)MyApplication.exe.config" />
		</Component>
	</ComponentGroup>
</Fragment>
  • Directory 产品的安装目录。第一个Directory Id=TARGETDIR 是根目录,每一个wix工程都会有这个目录元素,第二个Id=ProgramFilesFolder 对应的就是C:\Program Files\ ,64位的就在x86下,第三个元素就是我们自己的应用程序所在的文件夹,最终形成就是c:\Program Files\WxiSetup 在默认的提示中 也可以看见在Component 中添加安装文件,资源,注册表等。这里可以看见最里面的Directory的Id=INSTALLFOLDER是和第二个Fragment中的ComponentGroup的Directory属性是一致的。在Component中就是每一个你需要安装的单元以及它的位置。

$(var.MyApplication.TargetPath)是wix引用变量
$(var.MyApplication.TargetDir)指引用项目的Debug或者Release文件夹,后面的MyApplication.exe指文件夹中的文件,所有引用的Dll文件都需要手动添加

5、添加引导UI界面,需要引用WixUIExtension.dll,该文件在WiX Toolset build tools的安装目录bin文件夹下

在这里插入图片描述
在Product下添加UI节点

<UI> <UIRef Id="WixUI_InstallDir" /></UI>
<Property Id="WIXUI_INSTALLDIR" Value="INSTALLFOLDER" />

Wix提供了5种UI风格

  • WixUI_Advanced

  • WixUI_FeatureTree

  • WixUI_InstallDir 带有选择安装目录

  • WixUI_Minimal 简洁安装

  • WixUI_Mondo 自定义模块安装

  • Property安装配置特性。
      Id: 属性的名称 。WIXUI_INSTALLDIR只有存在选择安装路径界面存在是才有用,显示没人安装目录
      Value: 属性的参数,代码为目录Directory节点的Id
    常用Id参数

  • WIXUI_INSTALLDIR 安装默认路径

  • WIXUI_EXITDIALOGOPTIONALTEXT 安装成功界面显示文字

  • WIXUI_EXITDIALOGOPTIONALCHECKBOXTEXT 安装成功界面添加复选框(例安装完成打开软件)

  • WixShellExecTarget 执行事件的目标,需要和执行事件一起使用,在接下来的文章回讲到

6、修改程序图标与控制面板图标

在Product下添加Icon节点

<Icon Id="App.ico" SourceFile="$(var.MyApplication.TargetDir)\App.ico" />
  • Icon 程序图标。

使用Property节点修改控制面板图标

<Property Id="ARPPRODUCTICON" Value="App.ico" />

7、 右键WxiSetup生成,在Debug或者Release文件夹下生成安装包

在这里插入图片描述
点击msi文件安装

完整代码:

<?xml version="1.0" encoding="UTF-8"?>
<!-- The name of the product -->
<?define Name = "MyApplication" ?>
<!-- The Description of the product -->
<?define Description = "MyApplication is a winform desktop application." ?>
<!-- The manufacturer, for setup package publisher and folder info -->
<?define Manufacturer = "MyApplication Company Name" ?>
<!-- The version number of this setup package-->
<?define Version = "1.0.0" ?>
<!-- UpgradeCode must be unique and not changed once the first version of the program is installed. -->
<?define UpgradeCode = "{abaddda2-473c-4b8c-8d7a-7b6fad502cd4}" ?>
<!-- The name of the Cabinet -->
<?define CabName = "MyApplication.cab" ?>

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension" >
	<Product Id="*" Name="$(var.Name)" Language="2052" Version="$(var.Version)"
			 Manufacturer="$(var.Manufacturer)" UpgradeCode="$(var.UpgradeCode)">
		<Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />

		<!-- 程序图标 -->
		<Icon Id="App.ico" SourceFile="$(var.MyApplication.TargetDir)\App.ico" />
		<!-- 控制面板引用程序图标 -->
		<Property Id="ARPPRODUCTICON" Value="App.ico" />
		
		<!-- 允许升级和防止降级 -->
		<MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
		<!--将cab文件嵌入 msi文件-->
		<Media Id="1" Cabinet="$(var.CabName)" EmbedCab="yes" />
		<!--<MediaTemplate />-->

		<Feature Id="ProductFeature" Title="WxiSetup" Level="1">
			<ComponentGroupRef Id="ProductComponents" />

			<!-- 安装目录 -->

			<!--<ComponentRef Id="Config.xml"/>
			<ComponentRef Id="myapplication.exe"/>
			-->
			<ComponentRef Id="ApplicationShortcut" />
			<ComponentRef Id="DesktopFolderShortcut" />
			<!--<ComponentGroupRef Id="ComponentGroupDesktopShortcut" />-->
			<ComponentRef Id="Config.xml"/>
		</Feature>
		
		<UI>
			<UIRef Id="WixUI_InstallDir" />
		</UI>
		
		<WixVariable Id="WixUILicenseRtf" Value="license.rtf" />
		<!--<WixVariable Id="WixUIDialogBmp" Value="bmg.bmp"/>
		<WixVariable Id="WixUIBannerBmp" Value="top.bmp"/>-->

		<!-- Property 安装成功或显示
		WIXUI_INSTALLDIR 选择安装路径
		WIXUI_EXITDIALOGOPTIONALTEXT 显示文字
		WIXUI_EXITDIALOGOPTIONALCHECKBOXTEXT 添加复选框是否直接打开软件
		WixShellExecTarget 执行事件的目标
		-->
		<Property Id="WIXUI_INSTALLDIR" Value="INSTALLFOLDER" />

		<Property Id="WIXUI_EXITDIALOGOPTIONALTEXT" Value="Thank you for installing this product." />
		<Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOXTEXT" Value="Launch this Application " />

		<Property Id="WixShellExecTarget" Value="[#myapplication.exe]" />
		<CustomAction Id="LaunchApplication" BinaryKey="WixCA" DllEntry="WixShellExec" Impersonate="yes" />

		<!-- WixUI模板
		WixUI_Minimal 简洁模板,直接安装
		WixUI_Mondo 自定义模板,选择安装模块
		WixUI_InstallDir 安装选择路径-->
		<UIRef Id="WixUI_Common" />
	</Product>

	<Fragment>
		<Directory Id="TARGETDIR" Name="SourceDir">
			<Directory Id="ProgramFilesFolder" Name="PFiles">
				<Directory Id="INSTALLFOLDER" Name="$(var.Name)" />
			</Directory>
	</Fragment>

	<Fragment>
		<!-- Component 属性 
			Permanent='yes' 卸载时文件不删除 -->
		<ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
			<Component Id="ProductComponent">
				<File Id="myapplication.exe" Source="$(var.MyApplication.TargetDir)MyApplication.exe" />
				<File Id="myapplication.exe.config" Source="$(var.MyApplication.TargetDir)MyApplication.exe.config" />
			</Component>
		</ComponentGroup>
	</Fragment>
</Wix>

Wix 软件打包(二) 自定义安装界面和行为

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值