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

时间:2011-08-30 16:54:18

标签: c# installer windows-installer custom-action

我有一个自定义操作,可以通过Windows Installer从受信任的根证书添加/删除证书。我通过使用CustomAction

来实现这一目标

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

现在,Windows Installer始终报告成功响应,即使它失败了。

3 个答案:

答案 0 :(得分: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;
            }
         }
    }
}

注意:这是与WIX兼容的自定义操作。我找到WIX以允许更多地控制MSI创建。

答案 1 :(得分:3)

尝试抛出InstallException。在这种情况下,安装程序将检测安装和回滚操作的错误。

   public override void Commit(IDictionary savedState)
   {
      base.Commit(savedState);
      Console.WriteLine("Commit ...");
      // Throw an error if a particular file doesn't exist.
      if(!File.Exists("FileDoesNotExist.txt"))
         throw new InstallException();
      // Perform the final installation if the file exists.
   }

答案 2 :(得分:2)

这只能通过返回1602从win32 DLL或VBScript自定义操作完成。如果您使用的是EXE或安装程序类操作,则任何非零返回值都将被视为失败。

相关问题