找回密码
 立即注册
首页 业界区 业界 使用WiX创建Windows应用安装包

使用WiX创建Windows应用安装包

恿榫 2025-11-12 22:35:03
参考:官方教程
WiX 工具集(简称 WiX)用于构建 Windows 安装程序,它是构建工具、运行时工具和库的集合,不只是制作基本的安装包,还可以安装IIS网站、创建SQL Server、在Windows防火墙中注册例外。
安装 Wix 工具集


  • 无Visual Studio:用命令行安装 Wix 工具集。
    1. dotnet tool install --global wix
    复制代码
  • 有Visual Studio:安装 HeatWave for VS2022 扩展。安装后,重启 Visual Studio,你将看到可用的新项目模板。
添加MSI Package项目

添加 Wix 安装项目。在 解决方案资源管理器 中右键单击解决方案,然后选择“ 添加新<Feature Id="Main">
<WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
<Feature Id="Main">
<Component>
<Component>
  <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
</Component><File Source="MyApplication.exe" />
</Component><ComponentGroupRef Id="AppComponents" />
</Feature>
</WixLocalization><ComponentGroupRef Id="ExampleComponents" />
</Feature>><Feature Id="Main">
<WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
<Feature Id="Main">
<Component>
<Component>
  <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
</Component><File Source="MyApplication.exe" />
</Component><ComponentGroupRef Id="AppComponents" />
</Feature>
</WixLocalization><ComponentGroupRef Id="ExampleComponents" />
</Feature>项目
1.png

选择MSI Package,下一步,命名项目为.Installer。
WiX项目介绍

HeatWave中的 MSI Package (WiX v4) 模板给出了4个文件:

  • Package.wxs
  • Folders.wxs
  • ExampleComponents.wxs
  • Package.en-us.wxl
文件扩展名

HeatWave为我们生成的文件有两个扩展:

  • .wxs : WiX源文件。
  • .wxl : WiX本地化文件。
Package.wxs文件

WiX源文件是XML文件,根元素就是WiX,命名空间是 http://wixtoolset.org/schemas/v4/wxs。所有WiX源文件的根元素和命名空间都是相同的。
Package 元素

Package元素表示MSI包,只能有一个Package元素。HeatWave模板填写了所有MSI包所必需的属性,例如:
  1. [/code]
  2. [list]
  3. [*]Id 属性设置包的全球唯一ID,建议以公司名称作为前缀。在新开发项目中用此属性代替UpgradeCode,不再手动设置UpgradeCode。
  4. [*]Name 属性设置包的名称。该名称是在Windows Installed apps 列表中显示的名称。
  5. [*]Manufacturer 属性设置软件的公司的名称。此字符串也显示在Windows Installed apps 列表中。
  6. [*]Version 属性设置包的版本。包版本是管理包升级的重要部分。
  7. [/list][size=3]MajorUpgrade 元素[/size]
  8. [code]
复制代码
当你安装一个更高的版本时,之前的版本会首先被删除。

  • DowngradeErrorMessage 属性指定一个消息,当用户试图安装一个比已经安装的版本低的版本时显示。!(loc.DowngradeError) 语法是对本地化字符串的引用。
  • AllowSameVersionUpgrades 属性设置为no(默认)时,具有相同版本和UpgradeCode,但产品代码不同(产品代码每次构建时自动更新)的产品被视为两个不同产品,将被安装为两个应用。当设置为yes时,将被视为同一个产品,重复安装将视为升级。
  • IgnoreLanguage 属性设置为no(默认)时,不同语言的安装包将被安装为不同应用,设置为yes时,安装不同语言安装包将被视为升级。
Feature元素

Feature元素表示控制安装的内容。
  1. <Feature Id="Main">
  2. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  3. <Feature Id="Main">
  4. <Component>
  5. <Component>
  6.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  7. </Component><File Source="MyApplication.exe" />
  8. </Component><ComponentGroupRef Id="AppComponents" />
  9. </Feature>
  10. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  11. </Feature>
复制代码
ComponentGroupRef 元素

ComponentGroupRef元素表示引用具有相同Id的ComponentGroup元素,等同于把ComponentGroup元素填充到Feature元素。
另一种引用方式是通过属性引用,例如ComponentGroup元素有一个Directory属性,它的值是所引用的Directory元素的ID,表示Directory元素指定的目录作为组件组中所有文件的父目录。
  1. [/code][size=4]Folders.wxs文件[/size]
  2. [size=3]Fragment元素[/size]
  3. Fragment元素中的内容用于被引用。单个 .wxs 文件中可以包含多个 Fragment。
  4. [size=3]StandardDirectory元素[/size]
  5. [code]
复制代码
StandardDirectory 元素使示标准目录之一作为包目录的父目录,通过Id属性指定标准目录,可用的ID参考,其中有些是MSI系统文件夹属性ID,而有些不是,例如ProgramFiles6432Folder,它会根据安装包的位数变为标准MSI目录,例如,对于32位包,将解析为ProgramFilesFolder,对应路径C:\Program Files (x86),对于64位包,将解析为ProgramFiles64Folder,对应路径C:\Program Files。
Directory 元素
  1. [/code]Directory 元素将创建一个新目录:
  2. [list]
  3. [*]Id属性指定被引用时的ID。
  4. [*]Name是目录的名称。
  5. [*]!(bind.Property.Manufacturer)表示引用Package 元素的Manufacturer属性。
  6. [*]!(bind.Property.ProductName)表示Package 元素的Name属性。
  7. [/list]等效于:
  8. [code]
复制代码
ExampleComponents.wxs文件

File元素

表示一个文件。
  1. [/code]
  2. [list]
  3. [*]Source属性指定文件的名称。
  4. [*]示例项目是把自身的ExampleComponents.wxs文件当作安装文件,实际需改成应用程序。
  5. [/list][size=4]Package.en-us.wxl文件[/size]
  6. 这是一个本地化文件,可以添加多个本地化文件。
  7. [code]<Feature Id="Main">
  8. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  9. <Feature Id="Main">
  10. <Component>
  11. <Component>
  12.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  13. </Component><File Source="MyApplication.exe" />
  14. </Component><ComponentGroupRef Id="AppComponents" />
  15. </Feature>
  16. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  17. </Feature>
复制代码

  • 由于本地化文件不是 WiX 源文件,因此它们使用不同的根元素和命名空间。
  • Culture 属性指定本地化使用的语言和区域。
  1. [/code]
  2. [list]
  3. [*]本地化文件主要包含一堆字符串。每个字符串都有一个 id 和一个值,该值是指定的区域性中本地化的字符串。
  4. [/list]支持在任何硬编码字符串的地方引用本地化字符串,例如,在 Package.wxs 中使用 DowngradeError 字符串为 MajorUpgrade 元素提供错误消息:
  5. [code]
复制代码
实操

以下例子为MyApplication项目创建安装包:
2.png

对需要生成安装包的项目创建发布:
3.png

添加项目引用

在MSI Pachage项目中,添加对 MyApplication 项目的引用。右键单击 解决方案资源管理器中的安装项目,然后选择“ 添加<Feature Id="Main">
<WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
<Feature Id="Main">
<Component>
<Component>
  <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
</Component><File Source="MyApplication.exe" />
</Component><ComponentGroupRef Id="AppComponents" />
</Feature>
</WixLocalization><ComponentGroupRef Id="ExampleComponents" />
</Feature>><Feature Id="Main">
<WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
<Feature Id="Main">
<Component>
<Component>
  <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
</Component><File Source="MyApplication.exe" />
</Component><ComponentGroupRef Id="AppComponents" />
</Feature>
</WixLocalization><ComponentGroupRef Id="ExampleComponents" />
</Feature>项目引用”。
项目现在应包括一个 ProjectReference 元素,如下所示:
  1. <Feature Id="Main">
  2. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  3. <Feature Id="Main">
  4. <Component>
  5. <Component>
  6.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  7. </Component><File Source="MyApplication.exe" />
  8. </Component><ComponentGroupRef Id="AppComponents" />
  9. </Feature>
  10. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  11. </Feature><Feature Id="Main">
  12. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  13. <Feature Id="Main">
  14. <Component>
  15. <Component>
  16.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  17. </Component><File Source="MyApplication.exe" />
  18. </Component><ComponentGroupRef Id="AppComponents" />
  19. </Feature>
  20. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  21. </Feature><Feature Id="Main">
  22. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  23. <Feature Id="Main">
  24. <Component>
  25. <Component>
  26.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  27. </Component><File Source="MyApplication.exe" />
  28. </Component><ComponentGroupRef Id="AppComponents" />
  29. </Feature>
  30. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  31. </Feature><Feature Id="Main">
  32. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  33. <Feature Id="Main">
  34. <Component>
  35. <Component>
  36.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  37. </Component><File Source="MyApplication.exe" />
  38. </Component><ComponentGroupRef Id="AppComponents" />
  39. </Feature>
  40. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  41. </Feature>
复制代码
添加项目引用起到以下作用:

  • 确保应用项目在包项目之前生成,以便在生成包时应用本身可用。
  • 为包项目提供某种方法来查找应用项目的输出。
修改 Package 文件

修改Package元素的Id属性Name属性和Manufacturer属性。
嵌入Cabinet 文件

MSI Package项目构建后生成三个文件:

  • cab1.cab
  • WixTutorialPackage.msi
  • WixTutorialPackage.wixpdb
.cab是一个Cabinet 文件,包含了被安装的程序,与.msi文件组成了.msi包。.wixpdb文件与.pdb 文件一样,是一个调试文件,不是实际.msi包输出的一部分,可以被忽略。
WiX 的默认设值是将 cabinet 文件保持在 .msi 文件的外部,改变设置可以将 cabinet 文件嵌入到 .msi 文件本身中。在 WiX 中,MediaTemplate 元素控制如何生成和嵌入cabinet文件。在Package元素中添加MediaTemplate 元素,并设置EmbedCab属性为yes,如下所示:
  1. <Feature Id="Main">
  2. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  3. <Feature Id="Main">
  4. <Component>
  5. <Component>
  6.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  7. </Component><File Source="MyApplication.exe" />
  8. </Component><ComponentGroupRef Id="AppComponents" />
  9. </Feature>
  10. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  11. </Feature><Feature Id="Main">
  12. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  13. <Feature Id="Main">
  14. <Component>
  15. <Component>
  16.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  17. </Component><File Source="MyApplication.exe" />
  18. </Component><ComponentGroupRef Id="AppComponents" />
  19. </Feature>
  20. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  21. </Feature><Feature Id="Main">
  22. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  23. <Feature Id="Main">
  24. <Component>
  25. <Component>
  26.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  27. </Component><File Source="MyApplication.exe" />
  28. </Component><ComponentGroupRef Id="AppComponents" />
  29. </Feature>
  30. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  31. </Feature><Feature Id="Main">
  32. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  33. <Feature Id="Main">
  34. <Component>
  35. <Component>
  36.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  37. </Component><File Source="MyApplication.exe" />
  38. </Component><ComponentGroupRef Id="AppComponents" />
  39. </Feature>
  40. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  41. </Feature><Feature Id="Main">
  42. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  43. <Feature Id="Main">
  44. <Component>
  45. <Component>
  46.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  47. </Component><File Source="MyApplication.exe" />
  48. </Component><ComponentGroupRef Id="AppComponents" />
  49. </Feature>
  50. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  51. </Feature><Feature Id="Main">
  52. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  53. <Feature Id="Main">
  54. <Component>
  55. <Component>
  56.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  57. </Component><File Source="MyApplication.exe" />
  58. </Component><ComponentGroupRef Id="AppComponents" />
  59. </Feature>
  60. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  61. </Feature><Feature Id="Main">
  62. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  63. <Feature Id="Main">
  64. <Component>
  65. <Component>
  66.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  67. </Component><File Source="MyApplication.exe" />
  68. </Component><ComponentGroupRef Id="AppComponents" />
  69. </Feature>
  70. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  71. </Feature><Feature Id="Main">
  72. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  73. <Feature Id="Main">
  74. <Component>
  75. <Component>
  76.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  77. </Component><File Source="MyApplication.exe" />
  78. </Component><ComponentGroupRef Id="AppComponents" />
  79. </Feature>
  80. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  81. </Feature><Feature Id="Main">
  82. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  83. <Feature Id="Main">
  84. <Component>
  85. <Component>
  86.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  87. </Component><File Source="MyApplication.exe" />
  88. </Component><ComponentGroupRef Id="AppComponents" />
  89. </Feature>
  90. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  91. </Feature><Feature Id="Main">
  92. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  93. <Feature Id="Main">
  94. <Component>
  95. <Component>
  96.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  97. </Component><File Source="MyApplication.exe" />
  98. </Component><ComponentGroupRef Id="AppComponents" />
  99. </Feature>
  100. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  101. </Feature><Feature Id="Main">
  102. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  103. <Feature Id="Main">
  104. <Component>
  105. <Component>
  106.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  107. </Component><File Source="MyApplication.exe" />
  108. </Component><ComponentGroupRef Id="AppComponents" />
  109. </Feature>
  110. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  111. </Feature><Feature Id="Main">
  112. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  113. <Feature Id="Main">
  114. <Component>
  115. <Component>
  116.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  117. </Component><File Source="MyApplication.exe" />
  118. </Component><ComponentGroupRef Id="AppComponents" />
  119. </Feature>
  120. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  121. </Feature><Feature Id="Main">
  122. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  123. <Feature Id="Main">
  124. <Component>
  125. <Component>
  126.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  127. </Component><File Source="MyApplication.exe" />
  128. </Component><ComponentGroupRef Id="AppComponents" />
  129. </Feature>
  130. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  131. </Feature>
复制代码
重命名 ExampleComponents

ExampleComponents是模板的示例,应该重命名为合适的名称。
以下位置需要重命名:

  • 将ExampleComponents.wxs重命名为AppComponents.wxs。
  • 打开AppComponents.wxs文件,将ComponentGroup的ID修改为AppComponents:
    1. [/code]
    2. [*]打开Package.wxs文件,更新 ComponentGroupRef 的 ID:
    3. [code]<Feature Id="Main">
    4. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
    5. <Feature Id="Main">
    6. <Component>
    7. <Component>
    8.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
    9. </Component><File Source="MyApplication.exe" />
    10. </Component><ComponentGroupRef Id="AppComponents" />
    11. </Feature>
    12. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
    13. </Feature>
    复制代码
修改安装文件引用

打开AppComponents.wxs文件,修改内容如下:
  1. <Feature Id="Main">
  2. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  3. <Feature Id="Main">
  4. <Component>
  5. <Component>
  6.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  7. </Component><File Source="MyApplication.exe" />
  8. </Component><ComponentGroupRef Id="AppComponents" />
  9. </Feature>
  10. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  11. </Feature>
复制代码
WiX为每个引用项目的输出目录添加到绑定路径,绑定路径是 WiX 搜索打包的文件的路径。因此只需填写应用程序文件名,而不用填详细路径。
虽然不需要,但也可以指定绝对路径或相对路径,并且可以使用WiX预处理器变量引用具有相同名称的MSBuild 属性。例如以下示例中的$(Configuration)根据不同的解决方案配置获得路径:
  1. <Feature Id="Main">
  2. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  3. <Feature Id="Main">
  4. <Component>
  5. <Component>
  6.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  7. </Component><File Source="MyApplication.exe" />
  8. </Component><ComponentGroupRef Id="AppComponents" />
  9. </Feature>
  10. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  11. </Feature>
复制代码
Configuration是一个预处理器变量。
File元素只添加单个文件。通常应用程序包含多个文件,添加多个文件需使用Files元素,如下:
  1. <Feature Id="Main">
  2. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  3. <Feature Id="Main">
  4. <Component>
  5. <Component>
  6.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  7. </Component><File Source="MyApplication.exe" />
  8. </Component><ComponentGroupRef Id="AppComponents" />
  9. </Feature>
  10. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  11. </Feature><Feature Id="Main">
  12. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  13. <Feature Id="Main">
  14. <Component>
  15. <Component>
  16.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  17. </Component><File Source="MyApplication.exe" />
  18. </Component><ComponentGroupRef Id="AppComponents" />
  19. </Feature>
  20. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  21. </Feature>
复制代码
如果要保留某个文件卸载时不删除,比如配置文件,通过给Component元素添加Permanent="yes",表明此Component卸载时不被删除:
  1. <Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
  2.         <Fragment>
  3.                 <ComponentGroup Id="AppComponents" Directory="INSTALLFOLDER">
  4.                         <?define PublishDir = "$(MyApplication.ProjectDir)\bin\Release\net8.0\publish" ?>
  5.                         <Files Include="$(PublishDir)\**">
  6.                                 <Exclude Files="$(PublishDir)\appsettings.json"/>
  7.                         </Files>
  8.                         <Component Permanent="yes">
  9.                                 <File Source="$(PublishDir)\appsettings.json" />
  10.                         </Component>
  11.                 </ComponentGroup>
  12.         </Fragment>
  13. </Wix>
复制代码
安装为服务

添加NuGet包:

  • WixToolset.Util.wixext
4.png

修改AppComponents.wxs文件。示例:
  1. <Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
  2.         <Fragment>
  3.                 <ComponentGroup Id="AppComponents" Directory="INSTALLFOLDER">
  4.                         <?define PublishDir = "$(MyApplication.ProjectDir)\bin\Release\net8.0\publish" ?>
  5.                         <Files Include="$(PublishDir)\**">
  6.                                 <Exclude Files="$(PublishDir)\appsettings.json"/>
  7.                         </Files>
  8.                         <Component Permanent="yes">
  9.                                 <File Source="$(PublishDir)\appsettings.json" />
  10.                         </Component>
  11.                 </ComponentGroup>
  12.         </Fragment>
  13. </Wix><Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
  14.         <Fragment>
  15.                 <ComponentGroup Id="AppComponents" Directory="INSTALLFOLDER">
  16.                         <?define PublishDir = "$(MyApplication.ProjectDir)\bin\Release\net8.0\publish" ?>
  17.                         <Files Include="$(PublishDir)\**">
  18.                                 <Exclude Files="$(PublishDir)\appsettings.json"/>
  19.                         </Files>
  20.                         <Component Permanent="yes">
  21.                                 <File Source="$(PublishDir)\appsettings.json" />
  22.                         </Component>
  23.                 </ComponentGroup>
  24.         </Fragment>
  25. </Wix>                                                                               
复制代码
缺少官方教程,以上是个人尝试出的可行方法:

  • 使用Exclude排除掉主程序文件,以便在后面的Component指定主程序文件。
  • 主程序文件使用KeyPath="yes",以便自动生成GUID。
ServiceInstall

主要配置参数:

  • Start:required
    确定何时启动服务。该属性的值必须是下列值之一:

    • auto:服务将在系统启动期间启动。
    • demand:当服务控制管理器调用 StartService 函数时,服务将启动。
    • disabled:服务不能再启动。

util:ServiceConfig

此元素是Util扩展中的元素,需要添加NuGet包:WixToolset.Util.wixext,并添加命名空间:http://wixtoolset.org/schemas/v4/wxs/util。

  • FirstFailureActionType:第一次失败时的操作
  • SecondFailureActionType:第二次失败时的操作
  • ThirdFailureActionType:后续失败时的操作
    可取以下值:

    • none:无操作(默认值)
    • reboot:重新启动计算机
    • restart:重新启动服务
    • runCommand:运行一个程序

  • RestartServiceDelayInSeconds:如果三个*ActionType属性中的任何一个是“restart”,则指定在执行此操作之前等待的秒数。
修改安装路径

如果需要修改安装路径,例如建立两层文件夹,修改Folders.wxs,示例如下:
  1. <Feature Id="Main">
  2. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  3. <Feature Id="Main">
  4. <Component>
  5. <Component>
  6.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  7. </Component><File Source="MyApplication.exe" />
  8. </Component><ComponentGroupRef Id="AppComponents" />
  9. </Feature>
  10. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  11. </Feature><Feature Id="Main">
  12. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  13. <Feature Id="Main">
  14. <Component>
  15. <Component>
  16.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  17. </Component><File Source="MyApplication.exe" />
  18. </Component><ComponentGroupRef Id="AppComponents" />
  19. </Feature>
  20. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  21. </Feature><Feature Id="Main">
  22. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  23. <Feature Id="Main">
  24. <Component>
  25. <Component>
  26.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  27. </Component><File Source="MyApplication.exe" />
  28. </Component><ComponentGroupRef Id="AppComponents" />
  29. </Feature>
  30. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  31. </Feature><Feature Id="Main">
  32. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  33. <Feature Id="Main">
  34. <Component>
  35. <Component>
  36.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  37. </Component><File Source="MyApplication.exe" />
  38. </Component><ComponentGroupRef Id="AppComponents" />
  39. </Feature>
  40. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  41. </Feature><Feature Id="Main">
  42. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  43. <Feature Id="Main">
  44. <Component>
  45. <Component>
  46.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  47. </Component><File Source="MyApplication.exe" />
  48. </Component><ComponentGroupRef Id="AppComponents" />
  49. </Feature>
  50. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  51. </Feature><Feature Id="Main">
  52. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  53. <Feature Id="Main">
  54. <Component>
  55. <Component>
  56.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  57. </Component><File Source="MyApplication.exe" />
  58. </Component><ComponentGroupRef Id="AppComponents" />
  59. </Feature>
  60. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  61. </Feature><Feature Id="Main">
  62. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  63. <Feature Id="Main">
  64. <Component>
  65. <Component>
  66.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  67. </Component><File Source="MyApplication.exe" />
  68. </Component><ComponentGroupRef Id="AppComponents" />
  69. </Feature>
  70. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  71. </Feature><Feature Id="Main">
  72. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  73. <Feature Id="Main">
  74. <Component>
  75. <Component>
  76.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  77. </Component><File Source="MyApplication.exe" />
  78. </Component><ComponentGroupRef Id="AppComponents" />
  79. </Feature>
  80. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  81. </Feature><Feature Id="Main">
  82. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  83. <Feature Id="Main">
  84. <Component>
  85. <Component>
  86.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  87. </Component><File Source="MyApplication.exe" />
  88. </Component><ComponentGroupRef Id="AppComponents" />
  89. </Feature>
  90. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  91. </Feature>        <Feature Id="Main">
  92. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  93. <Feature Id="Main">
  94. <Component>
  95. <Component>
  96.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  97. </Component><File Source="MyApplication.exe" />
  98. </Component><ComponentGroupRef Id="AppComponents" />
  99. </Feature>
  100. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  101. </Feature><Feature Id="Main">
  102. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  103. <Feature Id="Main">
  104. <Component>
  105. <Component>
  106.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  107. </Component><File Source="MyApplication.exe" />
  108. </Component><ComponentGroupRef Id="AppComponents" />
  109. </Feature>
  110. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  111. </Feature><Feature Id="Main">
  112. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  113. <Feature Id="Main">
  114. <Component>
  115. <Component>
  116.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  117. </Component><File Source="MyApplication.exe" />
  118. </Component><ComponentGroupRef Id="AppComponents" />
  119. </Feature>
  120. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  121. </Feature><Feature Id="Main">
  122. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  123. <Feature Id="Main">
  124. <Component>
  125. <Component>
  126.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  127. </Component><File Source="MyApplication.exe" />
  128. </Component><ComponentGroupRef Id="AppComponents" />
  129. </Feature>
  130. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  131. </Feature><Feature Id="Main">
  132. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  133. <Feature Id="Main">
  134. <Component>
  135. <Component>
  136.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  137. </Component><File Source="MyApplication.exe" />
  138. </Component><ComponentGroupRef Id="AppComponents" />
  139. </Feature>
  140. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  141. </Feature><Feature Id="Main">
  142. <WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
  143. <Feature Id="Main">
  144. <Component>
  145. <Component>
  146.   <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
  147. </Component><File Source="MyApplication.exe" />
  148. </Component><ComponentGroupRef Id="AppComponents" />
  149. </Feature>
  150. </WixLocalization><ComponentGroupRef Id="ExampleComponents" />
  151. </Feature>
复制代码
生成安装包

先对被安装的项目执行发布。
然后对安装包项目执行生成,将生成一个msi文件。
进阶

阻止包安装

假如要检测安装包是否在 Windows 的 Server 版本上运行,如果是,则显示一条消息并退出安装程序。Windows Installer 可以为我们做以上事。
属性

MSI 具有许多内置属性 ,这些属性允许我们检测运行包的 Windows 版本,以及一个名为 launch conditions 的工具,用于在不满足用户指定的条件时阻止包。
MsiNTProductType 属性具有以下值:
ValueMeaning1Windows 2000 Professional and later2Windows 2000 domain controller and later3Windows 2000 Server and later所以如果MsiNTProductType 属性不是1,则表明在WIndows Server上运行。
条件和表达式

Windows Installer的表达式语法类似Basic,相等运算符是=,不等运算符是。但WiX 语言是用 XML 表示的, 字符是特殊的,仅用于将元素的名称括起来,不能在其他地方使用它们,因此要改成使用<和>。对于MsiNTProductType<Feature Id="Main">
<WixLocalization xmlns="http://wixtoolset.org/schemas/v4/wxl" Culture="en-US">
<Feature Id="Main">
<Component>
<Component>
  <File Source="..\MyApplication\bin\$(Configuration)\MyApplication.exe" />
</Component><File Source="MyApplication.exe" />
</Component><ComponentGroupRef Id="AppComponents" />
</Feature>
</WixLocalization><ComponentGroupRef Id="ExampleComponents" />
</Feature>1,要写成MsiNTProductType <> 1。
编写启动条件

启动条件由Launch元素表示,该元素通常作为Package 元素的子元素。
  1. [/code]如果Condition属性中的条件表达式为false,则表明不满足启动条件,将弹出一个消息框,显示Message属性的内容。
  2. 例如:
  3. [code]
复制代码
当MsiNTProductType不为1,将弹出如下消息框:
5.png


来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册