You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Wix V5中如何将<Files>收割的文件设置为永久文件?

Wix V5 实现收割文件设为永久文件的方案

你可以通过以下两种方式批量给<Files>收割的文件对应的Component添加Permanent="yes"属性,实现卸载时不删除这些文件:

1. 在WiX源文件(.wxs)中直接配置

如果是在.wxs里用<Files>元素批量添加文件,可通过ComponentDefaults设置默认Component属性:

全局默认永久

在<Wix>根节点下添加ComponentDefaults,让所有自动生成的Component都继承Permanent="yes":

<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
  <!-- 全局设置所有Component默认永久 -->
  <ComponentDefaults Permanent="yes" />

  <Product Id="*" Name="MyApp" Version="1.0.0.0" Manufacturer="MyCorp" Language="1033">
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLFOLDER" Name="MyApp">
          <!-- 收割A/B下所有文件,生成的Component自动带Permanent="yes" -->
          <Files Include="A/B/**" />
        </Directory>
      </Directory>
    </Directory>
  </Product>
</Wix>

仅指定目录下的文件永久

如果只想让某目录下收割的文件永久,把ComponentDefaults放在对应<Directory>节点下,作用于当前目录及其子目录的Component:

<Directory Id="INSTALLFOLDER" Name="MyApp">
  <!-- 仅当前目录下的Component默认永久 -->
  <ComponentDefaults Permanent="yes" />
  <Files Include="A/B/**" />
</Directory>

2. 用Heat.exe收割时通过XSLT转换

如果是用Heat.exe命令行或项目文件的HarvestDirectory配置收割文件,可通过XSLT批量给Component添加属性:

步骤1:创建XSLT转换文件

新建AddPermanent.xslt文件,内容如下:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wix="http://wixtoolset.org/schemas/v4/wxs">
  <xsl:output method="xml" indent="yes" />

  <!-- 给所有Component添加Permanent="yes"属性 -->
  <xsl:template match="wix:Component">
    <xsl:copy>
      <xsl:attribute name="Permanent">yes</xsl:attribute>
      <xsl:apply-templates select="@*[name() != 'Permanent']" />
      <xsl:apply-templates select="node()" />
    </xsl:copy>
  </xsl:template>

  <!-- 其他元素原样保留 -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

步骤2:调用Heat时应用转换

命令行方式:

heat dir A/B -dr INSTALLFOLDER -out harvested-files.wxs -transform AddPermanent.xslt

项目文件(.wixproj)配置方式:

在<ItemGroup>中添加HarvestDirectory并指定转换文件:

<ItemGroup>
  <HarvestDirectory Include="A/B">
    <DirectoryRefId>INSTALLFOLDER</DirectoryRefId>
    <Transform>AddPermanent.xslt</Transform>
    <ComponentGroupName>PermanentFilesGroup</ComponentGroupName>
  </HarvestDirectory>
</ItemGroup>

注意事项

  • Permanent="yes"是作用于Component的,只要每个文件对应的Component设置了该属性,卸载时就不会被删除;
  • 如果后续安装新版本替换了这些文件,卸载新版本时同样不会删除,除非新版本把对应Component的Permanent设为no;
  • 若有不需要永久的Component,可单独给其设置Permanent="no"覆盖默认值。

内容的提问来源于stack exchange,提问作者Antony Nifosi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 06:36:21