Wix Toolset基础 —— 环境变量设置示例
返回目录索引
本文由简至繁介绍如何在安装包中配置环境变量。
关于Environment元素的属性说明见:Wix Toolset基础 —— 环境变量设置
本文示例代码:Jess.Sample.Setup.Environment
基础配置示例
本文环境变量设置功能实现的是:将软件安装目录添加到系统环境变量 Path 的末尾处。
首先先看下最简配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| <?xml version="1.0" encoding="UTF-8"?> <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension" >
<Product Id="*" Name="环境变量设置Sample" Language="2052" Codepage="936" Version="1.0.0.0" Manufacturer="Jess" UpgradeCode="{6C0C1D6D-80CF-4074-AC42-5ADBD7E4087F}"> <Package InstallerVersion="300" Compressed="yes" InstallScope="perMachine" SummaryCodepage="936" />
<MediaTemplate EmbedCab="yes" />
<UIRef Id="WixUI_Minimal"/> <Property Id="WIXUI_INSTALLDIR" Value="INSTALLLOCATION" />
<Feature Id="ProductFeature" Title="Wix环境变量设置" Level="1"> <ComponentGroupRef Id="ProductComponents" /> </Feature> </Product>
<Fragment> <Directory Id="TARGETDIR" Name="SourceDir"> <Directory Id="ProgramFiles64Folder"> <Directory Id="INSTALLLOCATION" Name="EnvSample"> </Directory> </Directory> </Directory> </Fragment>
<Fragment> <ComponentGroup Id="ProductComponents" > <Component Id="cmpEnvSample" Guid="{63B3AF3B-94BB-4D3D-8D24-A0772CA5CFAA}" Directory="INSTALLLOCATION"> <CreateFolder /> <Environment Id="EnvSamplePath" Action="set" Part="last" Name="Path" Permanent="no" System="yes" Value="[INSTALLLOCATION]" /> </Component> </ComponentGroup> </Fragment>
</Wix>
|
其核心就一句话:<Environment Id="EnvSamplePath" Action="set" Part="last" Name="Path" Permanent="no" System="yes" Value="[INSTALLLOCATION]" />
控制环境变量设置时机
软件安装过程中,环境变量的设置,可能需要在某一步骤之后,即需要控制Wix安装的Action顺序。
例如环境变量的写入需要在卸载之后,配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <?define ProductVersion="1.0.0.0" ?>
<Product Id="*" Name="环境变量设置Sample" Language="2052" Codepage="936" Version="1.0.0.0" Manufacturer="Jess" UpgradeCode="{6C0C1D6D-80CF-4074-AC42-5ADBD7E4087F}"> <Package InstallerVersion="300" Compressed="yes" InstallScope="perMachine" SummaryCodepage="936" />
<MediaTemplate EmbedCab="yes" />
<UIRef Id="WixUI_Minimal"/> <Property Id="WIXUI_INSTALLDIR" Value="INSTALLLOCATION" />
<Feature Id="ProductFeature" Title="Wix环境变量设置" Level="1"> <ComponentGroupRef Id="ProductComponents" /> </Feature>
<Upgrade Id="{6C0C1D6D-80CF-4074-AC42-5ADBD7E4087F}"> <UpgradeVersion OnlyDetect='no' Property='SELFFOUND' Minimum='$(var.ProductVersion)' IncludeMinimum='yes' Maximum='$(var.ProductVersion)' IncludeMaximum='yes' /> <UpgradeVersion OnlyDetect='no' Property='PREVIOUSFOUND' Minimum='0.0.0.1' IncludeMinimum='yes' Maximum='$(var.ProductVersion)' IncludeMaximum='yes' /> <UpgradeVersion OnlyDetect='no' Property='NEWERFOUND' Minimum='$(var.ProductVersion)' IncludeMinimum='yes' /> </Upgrade>
<InstallExecuteSequence> <RemoveExistingProducts Before='InstallInitialize' /> <WriteEnvironmentStrings /> </InstallExecuteSequence> </Product>
|
核心就是在InstallExecuteSequence中添加WriteEnvironmentStrings执行步骤,也可以直接设置Sequence属性:<WriteEnvironmentStrings Sequence="1" />
控制环境变量设置条件
在某些情况下,环境变量的设置需要添加条件判断,如通过Bootstrap安装时,外部传入的参数,标记不需要添加环境变量。
以下配置添加了EnableEnv属性控制是否设置环境变量:
1 2 3 4 5 6 7
| <InstallExecuteSequence> <WriteEnvironmentStrings>EnableEnv=1</WriteEnvironmentStrings> </InstallExecuteSequence>
<Property Id="EnableEnv" Value="1"></Property>
|
另外,也可以直接设置Suppress属性禁用环境变量设置<WriteEnvironmentStrings Suppress="yes" />。
理:奇葩史