我有一个自定义操作,可以通过 Windows 安装程序从受信任的根证书中添加/删除证书。我通过使用 CustomAction 来实现这一点

用户可能没有将证书添加到 TrustedRoots 的权限,或者他们可能选择“取消”,我如何回滚以前的操作,并告诉安装程序我已取消该过程?

就目前而言,即使失败,Windows 安装程序也始终报告成功响应。

最佳答案

您应该将自定义操作设置为返回类型为 ActionResult 的 a 函数,这样您就可以在发生取消或其他异常时返回失败类型。

using Microsoft.Deployment.WindowsInstaller;
namespace CustomAction1
{
    public class CustomActions
    {
        [CustomAction]
        public static ActionResult ActionName(Session session)
        {
            try
            {
                session.Log("Custom Action beginning");

                // Do Stuff...
                if (cancel)
                {
                    session.Log("Custom Action cancelled");
                    return ActionResult.Failure;
                }

                session.Log("Custom Action completed successfully");
                return ActionResult.Success;
            }
            catch (SecurityException ex)
            {
                session.Log("Custom Action failed with following exception: " + ex.Message);
                return ActionResult.Failure;
            }
         }
    }
}

注意:这是与 WIX 兼容的自定义操作。我发现 WIX 可以更好地控制 MSI 创建。

10-07 19:09
查看更多