如何在VS2010 Windows Installer中取消和回滚自定义操作?

goo*_*ate 4 c# installer windows-installer custom-action

我有一个自定义操作,通过Windows Installer从受信任的根证书添加/删除证书.我通过使用CustomAction实现了这一点

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

现在,Windows Installer即使失败也始终报告成功响应.

Sco*_*ger 6

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

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;
            }
         }
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:这是一个兼容WIX的自定义操作.我发现WIX允许更多地控制MSI创建.

  • @ makerofthings7:这是一个与wix兼容的自定义动作.没有在您的问题中看到Windows安装项目的具体提及. (3认同)