简体   繁体   English

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

[英]How do I cancel and rollback a custom action in VS2010 Windows Installer?

I have a custom action that adds/removes a certificate from the Trusted Root Certificates via the Windows Installer. 我有一个自定义操作,通过Windows Installer从受信任的根证书添加/删除证书。 I achieve this by using a CustomAction 我通过使用CustomAction实现了这一点

It's possible that the user may not have permissions to add the certificate to TrustedRoots, or they may select "Cancel", how do I roll back the previous actions, and tell the installer that I've cancelled the process? 用户可能没有权限将证书添加到TrustedRoots,或者他们可能选择“取消”,如何回滚以前的操作,并告诉安装程序我已取消该过程?

As it stands now the Windows Installer is always reporting a success response even if it fails. 现在,Windows Installer即使失败也始终报告成功响应。

You should set up your custom action to the a function with return type of ActionResult that way you can return the failure type if the cancel happens or another exception. 您应该将自定义操作设置为返回类型为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;
            }
         }
    }
}

NOTE: This is a WIX compatible custom action. 注意:这是一个兼容WIX的自定义操作。 I find WIX to allow for more control over the MSI creation. 我发现WIX允许更多地控制MSI创建。

Try to throw an InstallException. 尝试抛出InstallException。 In this case installer will detect thomething wrong with the installation and rollback actions. 在这种情况下,安装程序将检测安装和回滚操作的错误。

   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.
   }

This can be done only from win32 DLL or VBScript custom actions by returning 1602 . 这只能通过返回1602从win32 DLL或VBScript自定义操作完成。 If you are using an EXE or an installer class action, any non-zero return value will be treated as a failure. 如果您使用的是EXE或安装程序类操作,则任何非零返回值都将被视为失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用自定义许可证验证在VS2010中创建msi安装程序 - how to create msi installer in VS2010 with custom licence validation 如何在vs2010中修复损坏的构建管理器? - How do I fix a corrupted build manager in vs2010? 如何在VS2010中调试已发布的XBAP文件? - How do I debug a published XBAP file in VS2010? 如何在 VS2010 中禁用此代码视图设置? - How do I disable this code view setting in VS2010? VS2010如何在安装程序中打包DB文件 - How VS2010 packs DB files in installer 如何通过自定义卸载操作获取安装目录(由MSI安装,由VS2010创建)? - How to get the installation directory (installed with MSI, created with VS2010) at a custom uninstall action? VS2010 Building Installer安装失败 - VS2010 Building Installer Failing on Setup 如何设置在VS2010中启动Windows窗体应用程序时加载的窗体? (将启动对象设置为无效) - How do I set the form that loads on startup of my Windows Forms application in VS2010? (Setting startup object to no avail) 如何在Windows安装程序的自定义操作中获取当前用户名? - How do I get the current users name in a custom action for windows installer? 如何将安装目录从安装程序传递到自定义操作? - How do I pass installation dir from the installer to a custom action?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM