WiX安装选项--环境变量

    xiaoxiao2022-06-23  71

    系统环境变量由 Windows 定义并应用到所有计算机用户。对系统环境的更改将写入注册表,而且通常需要重启计算机才能生效。通常我们的程序中也会使用环境变量,如何在WiX设置环境变量呢?

    安装一个环境变量,我们就需要向Environment table中添加一条记录,其中有几个字段是必须设置的:

    Environment - public key to be referenced from other tables.Name - name of the environment variable we want to create.Value - value of new environment variable, which is formatted field.同时在InstallExecuteSequence table 中需要添加 WriteEnvironmentStrings 或者RemoveEnvironmentStrings 操作。

    在安装/删除程序的时候需要处理我们的环境变量:

    安装程序:

    如果相同名称的环境变量名称不存在,则创建一个变量:

    如果已经存在一个相同名称的变量:

    用新的值代替旧值.

    不修改旧值或者忽略新值

    在环境变量的值列表开头插入一个新的值.

    在环境变量的值列表末尾插入一个新的值.

    删除一个环境变量:

    不管它的值

    只有当它的值和提供的值匹配时.

    环境变量的类型:

    用户环境变量(used in per-user installation).

    系统环境变量(used in per-machine installation).

    卸载程序:

    不删除环境变量.

    删除环境变量.

    下面我们来看一个例子:

    <?xml version="1.0" encoding="UTF-8"?> <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"> <Product Id="a960cf35-0779-43e8-923b-35638f2bfc42" Name="Minimal" Language="2052" Version="1.0.0.0" Manufacturer="Geffzhang" UpgradeCode="0bf7e020-5bbd-4a06-8e39-e715999edbf5"> <Package InstallerVersion="200" Compressed="yes" Description="Minimal Windows Installer Sample" Comments="This installer database contains the logic and data required to install Minimal Windows Installer Sample."/> <Media Id="1" Cabinet="media1.cab" EmbedCab="yes" /> <Property Id="EnableEV" Value="1"></Property> <Directory Id="TARGETDIR" Name="SourceDir"> <Directory Id="ProgramFilesFolder"> <Directory Id="INSTALLLOCATION" Name="Minimal"> <Component Id="Component1" Guid="{1781A625-8ACB-45E7-A8BA-219D81760B2E}"> <CreateFolder /> <Environment Id="TestMinVar" Action="set" Part="all" Name="MinEnvVar" Permanent="no" System="yes" Value="8" /> </Component> </Directory> </Directory> </Directory> <Feature Id="ProductFeature" Title="Minimal" Level="1"> <ComponentRef Id="Component1" /> </Feature> <InstallExecuteSequence> <WriteEnvironmentStrings>EnableEV=1</WriteEnvironmentStrings> </InstallExecuteSequence> <Property Id="WIXUI_INSTALLDIR" Value="INSTALLLOCATION" /> <UIRef Id="WixUI_InstallDir" /> </Product> </Wix>

    这个例子创建了一个系统环境变量 TestMinVar 值是 "8"。编译并安装例子程序,在系统环境变量就可以看到TestMinVar这个环境变量了,卸载这个例子程序,环境变量TestMinVar也会被删除。

    Environment table 也是一个 formatted field,这也就是说环境变量的值可以是来自一个属性,另一个环境变量,或者是任何一个formatted 字符串,下面的例子就是把环境变量设置成INSTALLLOCATION:

    <Environment Id="TestMinVar" Action="set" Part="all" Name="MinEnvVar" Permanent="no" System="no" Value="[INSTALLLOCATION]" />

    下面这个例子是用新的值代替已经存在的值:

    <Environment Id="TestMinVar" Action="set" Part="all" Name="MinEnvVar" Permanent="yes" System="no" Value="123" />

    主要就是Permanent ="yes",下面的例子把Permanent="no", Part="last"表示把值附加到后面:

    <Environment Id="TestMinVar" Action="set" Part="last" Name="MinEnvVar" Permanent="no" System="no" Value="456" />

    本文来自云栖社区合作伙伴“doNET跨平台”,了解相关信息可以关注“opendotnet”微信公众号

    相关资源:wix3.11版本安装工具

    最新回复(0)