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

如何在取消/中止安装时运行自定义操作(CA)?含静默模式与状态检测

在安装取消/中止时执行自定义命令(支持静默模式)

Wix# 实现步骤

1. 编写自定义操作方法

创建包含命令执行逻辑的托管自定义操作,确保静默模式下无窗口交互:

using System.Diagnostics;
using Microsoft.Deployment.WindowsInstaller;

public class CustomActions
{
    [CustomAction]
    public static ActionResult RunOnCancelOrAbort(Session session)
    {
        // 执行目标命令,静默模式下不显示窗口
        var psi = new ProcessStartInfo(@"C:\YourPath\YourCommand.exe")
        {
            CreateNoWindow = true,
            UseShellExecute = false,
            RedirectStandardOutput = true // 可选,捕获输出用于日志排查
        };

        using var process = Process.Start(psi);
        process?.WaitForExit();
        
        // 记录命令执行状态到安装日志
        session.Log($"Command exit code: {process?.ExitCode ?? -1}");
        return ActionResult.Success;
    }
}

2. 注册自定义操作到安装序列

将自定义操作绑定到InstallFinalize之前的执行点,设置触发条件覆盖取消和中止场景,同时指定Deferred执行模式确保在系统上下文运行:

var project = new Project("YourApplicationSetup",
    new Dir(@"%ProgramFiles%\YourCompany\YourApp",
        new File("YourApp.exe")
    )
);

// 处理已安装后卸载/取消的情况
project.Actions.Add(new ManagedAction(CustomActions.RunOnCancelOrAbort,
    When.Before, Step.InstallFinalize,
    "Installed AND (REMOVE=\"ALL\" OR Canceled)")
    { Execute = Execute.Deferred, Impersonate = false });

// 处理全新安装时的取消/错误中止情况
project.Actions.Add(new ManagedAction(CustomActions.RunOnCancelOrAbort,
    When.Before, Step.InstallFinalize,
    "NOT Installed AND (Canceled OR Error)")
    { Execute = Execute.Deferred, Impersonate = false });

// 生成安装包
Compiler.BuildMsi(project);

Wix 原生XML方案

如果使用原生Wix,配置逻辑一致:

1. 定义自定义操作

<Binary Id="CustomActionsDll" SourceFile="path/to/your/CustomActions.dll" />
<CustomAction Id="RunOnCancelOrAbort" 
              BinaryKey="CustomActionsDll" 
              DllEntry="RunOnCancelOrAbort" 
              Execute="deferred" 
              Impersonate="no" />

2. 绑定到执行序列

<InstallExecuteSequence>
    <!-- 已安装状态下的取消/卸载触发 -->
    <Custom Action="RunOnCancelOrAbort" Before="InstallFinalize">
        Installed AND (REMOVE="ALL" OR Canceled)
    </Custom>
    <!-- 全新安装时的取消/错误中止触发 -->
    <Custom Action="RunOnCancelOrAbort" Before="InstallFinalize">
        NOT Installed AND (Canceled OR Error)
    </Custom>
</InstallExecuteSequence>

检测取消/中止状态

在自定义操作中,可通过Session的内置属性判断当前状态:

[CustomAction]
public static ActionResult RunOnCancelOrAbort(Session session)
{
    bool isCanceled = session["Canceled"] == "1";
    bool isAbortedByError = session["Error"] == "1";

    if (isCanceled)
    {
        session.Log("安装被用户主动取消");
    }
    else if (isAbortedByError)
    {
        session.Log("安装因错误中止");
    }

    // 执行命令逻辑...
    return ActionResult.Success;
}

关键属性说明

  • Canceled:值为1表示用户通过UI或命令行取消了安装(静默模式下msiexec /i ... /qn /cancel也会触发)。
  • Error:值为1表示安装因未处理的错误而中止。

内容的提问来源于stack exchange,提问作者R.P

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 16:33:18