创建自定义操作以启动应用程序并退出安装程序

创建自定义操作以启动应用程序并退出安装程序

本文介绍了创建自定义操作以启动应用程序并退出安装程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢StackOverflow,我昨天发现了如何向Visual Studio安装程序中添加自定义操作,以便在更新后启动程序。我现在面临的问题是,在安装程序结束时,程序确实会打开,但是直到我退出应用程序,安装程序才完成。

Thanks to StackOverflow I found out yesterday how to add a custom action to the Visual Studio Installer to start my program after an update. The problem I now face is that at the end of the installer the program does open but the installer never finishes until I exit my app.

有没有办法确保应用程序仅在用户单击完MSI软件包后才启动?
还是程序在安装程序完成时启动,但安装程序完成并退出?

Is there a way to ensure the app starts only after the user clicks finish on the MSI package?Or the program starts at finish of installer but installer completes and exits?

如果有必要,我正在运行Visual Studio 2010。

I am running Visual Studio 2010 in case it matters.

推荐答案

在进行了一次Google搜索之后,我发现Visual Studio Installer的自定义操作可能需要指向Installer类。因此,我在解决方案中创建了一个类型为class的新项目。我删除了class1.cs文件,并使用以下代码将安装程序类添加到了新项目中(注意:需要在某些时候使用security.permissions):

After some Googling, I found out that the custom action for the Visual Studio Installer might need to point to an Installer Class. So I created a new project of type class in my solution. I deleted the class1.cs file and added an installer class to the new project with the following code (mental note: need to use security.permissions at some point):

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Diagnostics;
using System.Security.Permissions;


namespace AppName
{
    [RunInstaller(true)]
    public partial class InstallerClass : System.Configuration.Install.Installer
    {
        public InstallerClass()
        {
            InitializeComponent();
        }

        public override void Commit(System.Collections.IDictionary savedState)
        {
            base.Commit(savedState);
            System.Diagnostics.Process.Start(Context.Parameters["TARGETDIR"].ToString() + "application.exe");
            // Very important! Removes all those nasty temp files.
            base.Dispose();
        }
    }
}

在添加InstallerClass之后,我右键单击安装程序项目,然后选择添加>项目输出并添加了安装程序类。然后,我右键单击已安装的项目,然后执行视图>自定义操作。我将安装程序类添加到安装和提交文件夹中(如果仅将其添加到提交,则会收到错误1001:找不到文件InstallState。由于覆盖提交,它将仅在提交时运行。显然,InstallState是在第2阶段创建的,因此如果不能同时失败,则将失败。)

After the InstallerClass was added, I right clicked on the installer project and selected Add > Project Output and added the installer class. I then right clicked on the installed project and did View > Custom Action. I added the installer class to both the Install and Commit folders (if you only add it to Commit, you will get an Error 1001: could not find file InstallState. because of the override commit, it will only run on commit. apparently InstallState is created at stage 2 so if if its not in both it will fail miserably).

您必须添加CustomActionData条目。为此,选择 InstallerClass的主要输出,然后转到属性选项卡。将以下内容粘贴到CustomActionData中:

You must add a CustomActionData entry. To do so, select the "Primary Output from InstallerClass" and go to the Properites tab. Paste the following in CustomActionData:

/TARGETDIR="[TARGETDIR]\"

添加完之后,应用程序在安装完成后可以正常运行,您可以关闭安装程序,而不必等待应用程序退出!

After that was added the app runs properly when the install finishes and you can close the installer instead of waiting on the app to exit!

我所需要的。

我注意到的一个问题是安装程序现在在ApplicationFolder中创建多个.tmp文件和一个.InstallState文件。我想知道是否还需要向安装程序类中添加一些东西,以摆脱这些无用的文件?

弄清楚如何获取摆脱临时文件。使用Dispose()更新了代码。

Figured out how to get rid of the temp files. Updated code with Dispose().

这篇关于创建自定义操作以启动应用程序并退出安装程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:39