MSI 安装程序取消不起作用

发布于 2024-12-07 19:59:29 字数 194 浏览 1 评论 0原文

我有一个 MSI 安装程序(设置项目),它实际上通过 system.diagnostic.process 调用 Windows 窗体 exe。

该表单实际上接受用户的输入来恢复 sql server 中的 .bak 文件。

但如果发生任何异常,我无法取消设置。即使单击安装程序的取消按钮,安装回滚也不会开始。

请建议如何处理。

I have an MSI installer (set-up project) which actually calls a Windows Forms exe through system.diagnostic.process.

This form actually takes input from the user to restore a .bak file in sql server.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最单纯的乌龟 2024-12-14 19:59:29

创建调用 Windows 窗体 exe 的新项目并向其添加安装程序类,或者仅将安装程序类添加到 Windows 窗体 exe(您必须更改其输出类型并对其进行一些修改,因此,例如,没有Main() 方法,或未设置启动对象,并且从安装操作内部调用您的表单)

安装程序类应如下所示:

[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);
    }

}

当您准备好安装程序项目时,转到安装项目自定义操作并添加主要输出您的安装程序项目进行安装,提交、回滚和卸载“文件夹”。

另一个有用的事情是获取应用程序的安装目录并将其路径传递给安装程序类。您将自定义操作的 CustomActionData 属性设置为,

/INSTALLDIR="[TARGETDIR]\"

并在安装程序类中使用以下命令获取目录:

Context.Parameters["INSTALLDIR"]

编辑:如果从 Install 方法中引发异常,安装将被取消。据我所知,这是在中间“取消”安装的唯一方法。您必须创建一个异常并抛出它。例如像这样:

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

当然,如果异常是由其他东西抛出的(我的意思是,不是您自愿“创建”的),回滚也应该开始。但是,如果您进行一些自定义更改,则必须从自定义操作的安装程序的安装方法中抛出它。

抱歉,如果我说得太详细了,但我已经解决了自己的一系列麻烦,所以如果我能帮助别人避免它,我会很高兴:)

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)

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

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

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文