简体   繁体   English

MSI安装程序取消无效

[英]MSI installer cancel not working

I have an MSI installer (set-up project) which actually calls a Windows Forms exe through system.diagnostic.process. 我有一个MSI安装程序(设置项目),该安装程序实际上是通过system.diagnostic.process调用Windows Forms exe的。

This form actually takes input from the user to restore a .bak file in sql server. 此表单实际上从用户处获取输入以在sql server中还原.bak文件。

But if any exception occur, I am not able to cancel the setup. 但是,如果发生任何异常,我将无法取消设置。 Even after clicking the cancel button of installer the installation roll-back will not start. 即使单击安装程序的取消按钮,安装回滚也不会开始。

Please suggest how to handle it. 请建议如何处理。

Create new project that calls your windows forms exe and add installer class to it, or just add installer class to your windows forms exe (you'll have to change it's output type and modify it a little, so, for example, there is no Main() method, or startup object is not set, and your form is called from inside of install action) 创建调用Windows窗体exe的新项目并向其添加安装程序类,或者仅将安装程序类添加至Windows窗体exe(您必须更改其输出类型并对其进行一些修改,因此,例如,没有Main()方法或未设置启动对象,并且从安装操作内部调用了表单)

Installer class should look like this: 安装程序类应如下所示:

[RunInstaller(true)]
public  partial class Installer1 : System.Configuration.Install.Installer
{
    public Installer1()
    {
        InitializeComponent();
    }
    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Install(IDictionary stateSaver)
    {
        base.Install(stateSaver);
        // Do your magic here, call your form, do your thing...

    }
 [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Commit(IDictionary savedState)
    {

        base.Commit(savedState);
    }

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Rollback(IDictionary savedState)
    {
        // if something goes wrong, it's here you correct it and rollback the system to its previous state and undo what you already changed
        base.Rollback(savedState);
    }

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Uninstall(IDictionary savedState)
    {
        // Here you undo changes when you cancel
        base.Uninstall(savedState);
    }

}

When you have your installer project ready, go to your setup project Custom Action and add primary output of your installer project to its install, commit, rollback and uninstall "folders". 准备好安装程序项目后,转到设置项目“自定义操作”,然后将安装程序项目的主要输出添加到其安装,提交,回滚和卸载“文件夹”中。

Another usefull thing is getting the directory in which your app is being installed and passing its path to your installer class. 另一个有用的事情是获取应用程序的安装目录,并将其路径传递给安装程序类。 you do this setting CustomActionData property of your custom action to 您可以将自定义操作的CustomActionData属性设置为

/INSTALLDIR="[TARGETDIR]\"

and in your installer class you get the directory using: 并在安装程序类中使用以下命令获取目录:

Context.Parameters["INSTALLDIR"]

EDIT: And installation will be canceled if an exception is thrown from within the Install method. 编辑:如果在Install方法中引发异常,则安装将被取消。 As far as I know it's the only way to "cancel" your installation in the middle. 据我所知,这是“取消”中间安装的唯一方法。 You have to create an exception and throw it., for example like this: 您必须创建一个异常并将其引发。例如:

If (SomethingWentWrong) throw new Exception("My exception description")

Of course, if the exception was thrown by something else (I mean, not willingly "created" by you) the rollback shoud also start. 当然,如果异常是由别的东西抛出的(我是说,不是您故意创建的),回滚也应开始。 But if you do some custom changes, it has to be thrown from within the custom action's installer's install method. 但是,如果您进行一些自定义更改,则必须从自定义操作的安装程序的install方法中抛出该更改。

Sorry if I was a bit too detailed but I got through my own set of troubles over this so I'll be happy if I can help someone to avoid it :) 抱歉,如果我太详细了,但是我在这方面遇到了麻烦,所以如果我能帮助某人避免它,我会很高兴的:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM