好的 👍 阿杰,我来给你一个 WiX 简介,涵盖它的定义、特点、应用场景和简单示例。


🛠️ WiX 简介

1️⃣ 什么是 WiX?

WiX (Windows Installer XML) 是微软支持的一个 开源工具集,用于通过 XML 文件来创建 Windows Installer(MSI、MSM、MSP)安装包。

  • 官网:https://wixtoolset.org
  • 由微软开发并开源(SourceForge → GitHub)
  • 适用于 .NET 应用、桌面应用、C++ 程序 等

2️⃣ WiX 的特点

  • 基于 XML:安装流程、文件、注册表、快捷方式等都通过 XML 配置
  • 生成 MSI:最终产物是标准的 Windows Installer 格式(.msi / .exe)
  • 灵活强大:可处理复杂的安装需求(服务注册、COM 注册、自定义动作)
  • 可集成:支持 MSBuild、Visual Studio、Azure DevOps CI/CD
  • 开源免费:MIT License,适合商业和个人项目

3️⃣ 基本组成

  • Product.wxs:定义产品信息(名称、版本、GUID)
  • Feature 元素:安装功能模块
  • Component 元素:实际安装的文件或资源
  • Directory 元素:安装目录结构
  • UI 支持:内置对话框,也可自定义

4️⃣ 简单示例

一个最小化的 Product.wxs 文件:

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Product Id="*" Name="MyApp" Language="1033" Version="1.0.0.0" Manufacturer="阿杰公司" UpgradeCode="PUT-GUID-HERE">
    <Package InstallerVersion="500" Compressed="yes" InstallScope="perMachine" />

    <MediaTemplate />

    <Feature Id="ProductFeature" Title="MyApp Feature" Level="1">
      <ComponentGroupRef Id="ProductComponents" />
    </Feature>
  </Product>

  <Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLFOLDER" Name="MyApp" />
      </Directory>
    </Directory>
  </Fragment>

  <Fragment>
    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
      <Component Id="MyAppFile" Guid="PUT-GUID-HERE">
        <File Id="MyAppExe" Source="bin\MyApp.exe" />
      </Component>
    </ComponentGroup>
  </Fragment>
</Wix>

编译命令:

candle Product.wxs   # 编译为 .wixobj
light Product.wixobj # 生成 MyApp.msi


5️⃣ 应用场景

  • Windows 桌面软件安装包
  • 自动安装依赖项(.NET、VC++ runtime)
  • 注册表项写入、服务安装
  • 企业级软件发布(支持升级、修复、卸载)

✅ 总结

  • WiX = 用 XML 描述安装逻辑 → 生成 MSI 安装包
  • 优点:灵活、开源、可集成、支持复杂安装
  • 缺点:学习曲线比 NSIS、Inno Setup 稍陡峭

👌 好的,阿杰。我来给你一个 WiX 入门教程项目,带你从零写一个 C# 应用并用 WiX 打包成 MSI 安装包。


🛠️ WiX 入门教程项目

1️⃣ 环境准备


2️⃣ 创建一个简单的 C# 应用

在 Visual Studio 中创建一个 Console App (.NET 6/7),代码如下:

using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello 阿杰! 欢迎使用 WiX 安装包测试程序 🚀");
            Console.ReadKey();
        }
    }
}

生成后会得到 bin\Release\net6.0\MyApp.exe


3️⃣ 创建 WiX 配置文件

新建 Product.wxs 文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Product Id="*" Name="MyApp" Language="1033" Version="1.0.0.0" 
           Manufacturer="阿杰公司" UpgradeCode="PUT-GUID-HERE">
    <Package InstallerVersion="500" Compressed="yes" InstallScope="perMachine" />

    <MediaTemplate />

    <Feature Id="ProductFeature" Title="MyApp Feature" Level="1">
      <ComponentGroupRef Id="ProductComponents" />
    </Feature>
  </Product>

  <Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLFOLDER" Name="MyApp" />
      </Directory>
    </Directory>
  </Fragment>

  <Fragment>
    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
      <Component Id="MyAppFile" Guid="PUT-GUID-HERE">
        <File Id="MyAppExe" Source="bin\Release\net6.0\MyApp.exe" />
      </Component>
    </ComponentGroup>
  </Fragment>
</Wix>

👉 注意PUT-GUID-HERE 需要替换成真实 GUID(可用 Visual Studio 或 PowerShell 生成):

[guid]::NewGuid()


4️⃣ 编译 WiX 安装包

在命令行中运行:

candle Product.wxs   # 编译 XML → Product.wixobj
light Product.wixobj -o MyApp.msi  # 链接生成 MSI

最终会得到 MyApp.msi 安装包。


5️⃣ 安装与测试

  • 双击 MyApp.msi,程序会安装到 C:\Program Files\MyApp
  • 打开安装目录,运行 MyApp.exe,你会看到:
Hello 阿杰! 欢迎使用 WiX 安装包测试程序 🚀


6️⃣ 进阶功能(后续可以尝试)

  • 添加 桌面快捷方式
  • 自动创建 开始菜单项
  • 写入 注册表键值
  • 安装 Windows 服务
  • 支持 升级 / 卸载 / 修复

✅ 现在你已经完成了一个 最小可用的 WiX 项目
C# 控制台程序 → WiX 配置 → MSI 安装包。

👌 明白了,阿杰,我给你写一个 WiX 进阶示例,实现 桌面快捷方式 + 卸载入口,完整可用。


🛠️ WiX 进阶示例:桌面快捷方式 + 卸载入口

假设你已经有一个 C# 程序 MyApp.exe,在 bin\Release\net6.0\ 下。


1️⃣ 修改 Product.wxs

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Product Id="*" Name="MyApp" Language="1033" Version="1.0.0.0"
           Manufacturer="阿杰公司" UpgradeCode="PUT-GUID-HERE">
    <Package InstallerVersion="500" Compressed="yes" InstallScope="perMachine" />

    <MediaTemplate />

    <!-- 安装功能 -->
    <Feature Id="ProductFeature" Title="MyApp Feature" Level="1">
      <ComponentGroupRef Id="ProductComponents" />
    </Feature>

    <!-- 卸载入口 -->
    <Property Id="ARPCONTACT" Value="阿杰公司" />
    <Property Id="ARPHELPLINK" Value="https://www.example.com/support" />
  </Product>

  <!-- 安装目录 -->
  <Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLFOLDER" Name="MyApp" />
      </Directory>
      <!-- 桌面目录 -->
      <Directory Id="DesktopFolder" Name="Desktop"/>
    </Directory>
  </Fragment>

  <!-- 文件与组件 -->
  <Fragment>
    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
      <Component Id="MyAppFile" Guid="PUT-GUID-HERE">
        <File Id="MyAppExe" Source="bin\Release\net6.0\MyApp.exe" KeyPath="yes" />

        <!-- 桌面快捷方式 -->
        <Shortcut Id="DesktopShortcut"
                  Directory="DesktopFolder"
                  Name="MyApp"
                  WorkingDirectory="INSTALLFOLDER"
                  Icon="MyAppIcon.ico"
                  IconIndex="0"
                  Advertise="no"
                  Target="[INSTALLFOLDER]MyApp.exe" />
      </Component>
    </ComponentGroup>
  </Fragment>

  <!-- 图标文件 -->
  <Fragment>
    <Icon Id="MyAppIcon.ico" SourceFile="MyAppIcon.ico" />
  </Fragment>
</Wix>


2️⃣ 关键点说明

  1. 桌面快捷方式
<Shortcut Id="DesktopShortcut"
          Directory="DesktopFolder"
          Name="MyApp"
          WorkingDirectory="INSTALLFOLDER"
          Icon="MyAppIcon.ico"
          IconIndex="0"
          Advertise="no"
          Target="[INSTALLFOLDER]MyApp.exe" />

  • Directory="DesktopFolder" → 安装到桌面
  • Target="[INSTALLFOLDER]MyApp.exe" → 目标程序
  • Icon → 快捷方式图标,可选
  1. 卸载入口
<Property Id="ARPCONTACT" Value="阿杰公司" />
<Property Id="ARPHELPLINK" Value="https://www.example.com/support" />

  • 这些属性会显示在 控制面板 → 卸载程序
  • ARPCONTACT → 联系人
  • ARPHELPLINK → 帮助链接
  1. KeyPath=”yes”
  • 指定组件主文件(主文件存在,则组件认为已安装)

3️⃣ 编译命令

candle Product.wxs      # 编译为 Product.wixobj
light Product.wixobj -o MyApp.msi  # 链接生成 MSI


4️⃣ 测试效果

  1. 双击 MyApp.msi 安装
  2. 桌面会出现 MyApp 快捷方式
  3. 控制面板 → 卸载程序,显示 阿杰公司 和支持链接
  4. 卸载时,桌面快捷方式会自动删除

5️⃣ 拓展功能(可选)

  • 多语言安装界面(UI)
  • 自定义安装对话框
  • 安装服务 / 注册表写入
  • 自动更新与升级 MSI

👌 明白了,阿杰。我帮你整理一个 完整可用的 WiX 安装包项目模板,包含:

  • C# 控制台程序
  • 桌面快捷方式
  • 卸载入口
  • 图标
  • 可直接在 Visual Studio 打开编译

🛠️ WiX 完整安装包项目模板

1️⃣ 项目结构

WiXDemo/
│
├─ MyApp/                 # C# 控制台程序
│   ├─ Program.cs
│   └─ MyAppIcon.ico      # 快捷方式图标
│
├─ WiXInstaller/          # WiX 项目
│   ├─ Product.wxs
│   └─ MyAppIcon.ico      # 同步图标
│
└─ README.md


2️⃣ C# 控制台程序(MyApp/Program.cs)

using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello 阿杰! 欢迎使用 WiX 安装包测试程序 🚀");
            Console.WriteLine("按任意键退出...");
            Console.ReadKey();
        }
    }
}

生成路径:MyApp/bin/Release/net6.0/MyApp.exe


3️⃣ WiX 安装包配置(WiXInstaller/Product.wxs)

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Product Id="*" Name="MyApp" Language="1033" Version="1.0.0.0"
           Manufacturer="阿杰公司" UpgradeCode="PUT-GUID-HERE">
    <Package InstallerVersion="500" Compressed="yes" InstallScope="perMachine" />

    <MediaTemplate />

    <Feature Id="ProductFeature" Title="MyApp Feature" Level="1">
      <ComponentGroupRef Id="ProductComponents" />
    </Feature>

    <!-- 卸载信息 -->
    <Property Id="ARPCONTACT" Value="阿杰公司" />
    <Property Id="ARPHELPLINK" Value="https://www.example.com/support" />
  </Product>

  <!-- 安装目录 -->
  <Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLFOLDER" Name="MyApp" />
      </Directory>
      <Directory Id="DesktopFolder" Name="Desktop"/>
    </Directory>
  </Fragment>

  <!-- 文件与组件 -->
  <Fragment>
    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
      <Component Id="MyAppFile" Guid="PUT-GUID-HERE">
        <File Id="MyAppExe" Source="..\MyApp\bin\Release\net6.0\MyApp.exe" KeyPath="yes" />

        <!-- 桌面快捷方式 -->
        <Shortcut Id="DesktopShortcut"
                  Directory="DesktopFolder"
                  Name="MyApp"
                  WorkingDirectory="INSTALLFOLDER"
                  Icon="MyAppIcon.ico"
                  IconIndex="0"
                  Advertise="no"
                  Target="[INSTALLFOLDER]MyApp.exe" />
      </Component>
    </ComponentGroup>
  </Fragment>

  <!-- 图标文件 -->
  <Fragment>
    <Icon Id="MyAppIcon.ico" SourceFile="MyAppIcon.ico" />
  </Fragment>
</Wix>


4️⃣ GUID 生成

在 PUT-GUID-HERE 处替换为真实 GUID:

[guid]::NewGuid()

  • 一个用于 Product Id
  • 一个用于 Component Id

5️⃣ 编译 WiX 安装包

打开命令行,进入 WiXInstaller 目录:

candle Product.wxs      # 编译 XML → Product.wixobj
light Product.wixobj -o MyApp.msi  # 链接生成 MSI

生成文件:MyApp.msi


6️⃣ 测试

  1. 双击 MyApp.msi 安装
  2. 桌面生成 MyApp 快捷方式
  3. 控制面板 → 卸载程序显示 阿杰公司 和支持链接
  4. 卸载自动删除桌面快捷方式

7️⃣ 可拓展功能(下一步)

  • 自定义安装 UI 界面
  • 多语言支持
  • 自动升级 / 修复 MSI
  • 安装 Windows 服务 / 注册表键值