系统环境变量由 Windows 定义并应用到所有计算机用户。对系统环境的更改将写入注册表,而且通常需要重启计算机才能生效。通常我们的程序中也会使用环境变量,如何在WiX设置环境变量呢?
安装一个环境变量,我们就需要向Environment table中添加一条记录,其中有几个字段是必须设置的:
同时在InstallExecuteSequence table 中需要添加 WriteEnvironmentStrings 或者RemoveEnvironmentStrings 操作。
在安装/删除程序的时候需要处理我们的环境变量:
下面我们来看一个例子:
<?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" />
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有