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

发布于 2024-09-07 22:41:37 字数 229 浏览 2 评论 0原文

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

有没有办法确保应用程序仅在用户单击 MSI 包上的“完成”后启动? 或者程序在安装程序完成时启动,但安装程序完成并退出?

我正在运行 Visual Studio 2010,以防万一。

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.

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?

I am running Visual Studio 2010 in case it matters.

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

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

发布评论

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

评论(3

甜`诱少女 2024-09-14 22:41:37

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

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 后,我右键单击安装程序项目并选择添加>项目输出并添加了安装程序类。然后我右键单击已安装的项目并执行“查看”>“自定义操作。我将安装程序类添加到 Install 和 Commit 文件夹中(如果只将其添加到 Commit 中,则会收到错误 1001:找不到文件 InstallState。由于覆盖提交,它只会在提交时运行。显然 InstallState 是在第 2 阶段创建,因此如果两者都不存在,它将惨败)。

您必须添加 CustomActionData 条目。为此,请选择“来自 InstallerClass 的主输出”并转到“属性”选项卡。将以下内容粘贴到 CustomActionData 中:

/TARGETDIR="[TARGETDIR]\"

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

正是我所需要的。感谢谷歌拯救了我的培根。

我注意到的一个问题是安装程序现在在我的 ApplicationFolder 中创建多个 .tmp 文件和一个 .InstallState 文件。我想知道是否需要在安装程序类中添加一些额外的内容来删除这些无用的文件?

弄清楚如何删除临时文件。使用 Dispose() 更新了代码。

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

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

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!

Just what I needed. Thank you Google for saving my bacon.

The one issue I noticed was the installer now creates multiple .tmp files and a .InstallState file in my ApplicationFolder. I am wondering if there is something in addition that needs to be added to the installer class to get rid of these useless files?

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

愚人国度 2024-09-14 22:41:37

我按照这里的说明进行操作:
http://msdn.microsoft.com/en-us/library/d9k65z2d.aspx

..并收到“错误 1001:找不到文件 InstallState”错误。

读完上面 ThaKidd 的回答后,我意识到我必须:
将安装程序类添加到 Install 和 Commit 文件夹中。

非常重要。只是将其留在这里供未来的访客使用(如果允许的话,我会添加评论......)

I followed the instructions here:
http://msdn.microsoft.com/en-us/library/d9k65z2d.aspx

..And got the "Error 1001: could not find file InstallState" error.

After reading ThaKidd's answer above, I realized that I would have to:
Add the installer class to both the Install and Commit folders.

Really important. Just leaving this here for future visitors (I would have added a comment if SO allowed me to...)

抽个烟儿 2024-09-14 22:41:37

这是回答问题并清理卸载事件中的所有文件的分步指南。我希望这可以帮助刚接触 Visual Studio 安装程序项目的人。

在您的解决方案 (.sln) 中,您应该至少有 2 个项目。一个是您的程序,另一个是设置。鉴于该问题,本指南不包括有关如何添加设置或主要输出的基础知识(如果您需要了解,请观看视频)。

请按照下列步骤操作:

  1. 右键单击主项目(不是设置)->添加->组件->安装程序类
  2. 为组件命名并单击“添加”
  3. 打开文件,单击切换到代码视图(或选择 ComponentName.cs -> ComponentName.Desinger.cs -> ComponentName - > ComponentName())
  4. 看一下下面的代码,添加 using 语句、提交和卸载方法。运行代码。
  5. 右键点击安装项目->查看->自定义操作
  6. 右键单击“安装”->添加自定义操作... ->应用程序文件夹->您项目的主要输出(活动)->好的->右键单击添加的文件并选择属性窗口(或F4)->将 CustomActionData 设置为 /TARGETDIR="[TARGETDIR]\"(末尾有一个反斜杠)并确保安装程序类设置为 True
  7. 对自定义操作重复上述步骤提交、回滚和卸载
  8. 重新构建安装程序
  9. 安装安装程序
  10. 您可以需要 System.Configuration.Install NuGet

完整代码

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
    
namespace Your_Namespace
{
    [RunInstaller(true)]
    public partial class ComponentName : System.Configuration.Install.Installer
    {
        public ComponentName()
        {
            InitializeComponent();
        }
    
        //Source: https://stackoverflow.com/questions/3172406/create-custom-action-to-start-application-and-exit-installer
        public override void Commit(System.Collections.IDictionary savedState)
        {
            base.Commit(savedState); 
            System.Diagnostics.Process.Start(Context.Parameters["TARGETDIR"].ToString() + "your main program.exe");
            //Remove temp files
            base.Dispose();
        }
    
        //Delete the file .InstallState (created when using custom actions in setup)
        //Could also delete this after the install (protected override void onAfterInstall) but not tested
        //Source: https://stackoverflow.com/questions/46786297/visual-studio-setup-project-remove-files-created-at-runtime-when-uninstall?rq=1
        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
            File.Delete(Context.Parameters["TARGETDIR"].ToString() + "your main program.InstallState");
            //Remove temp files
            base.Dispose();
        }
    }
}

Visual Studio 安装程序项目(2017、2019)的其他内容可能会在以后为您省去麻烦:

This is a step by step guide to answer the Question and clean up all files on an uninstall Event. I hope this can help people who are new to Visual Studio Installer Projects.

In your solution (.sln) you should have at least 2 projects. One being your program and the other one the setup. Given the Question this guide does not include basics on how to add a setup or the primary output(Here is a video if you need to catch up).

Follow these steps:

  1. Right click on the main project (not the setup) -> Add -> Component -> Installer Class
  2. Name the Component and click Add
  3. Open the file click on switch to code view (or select ComponentName.cs -> ComponentName.Desinger.cs -> ComponentName -> ComponentName())
  4. Take a look at the code below, add the using statements, the commit and uninstall method. Run the code.
  5. Right click on the setup project -> View -> Custom Actions
  6. Right click Install -> Add Custom Actions... -> Application Folder -> Primary Output from yourProject (Active) -> Ok -> Right click the added file and select properties Window (or F4) -> set CustomActionData to /TARGETDIR="[TARGETDIR]\" (one backslash at the end) and make sure that Installer Class is set to True
  7. Repeat the above step for the Custom Actions Commit, Rollback and Uninstall
  8. Rebuild the setup
  9. Install the setup
  10. You may need the System.Configuration.Install NuGet

Full Code

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
    
namespace Your_Namespace
{
    [RunInstaller(true)]
    public partial class ComponentName : System.Configuration.Install.Installer
    {
        public ComponentName()
        {
            InitializeComponent();
        }
    
        //Source: https://stackoverflow.com/questions/3172406/create-custom-action-to-start-application-and-exit-installer
        public override void Commit(System.Collections.IDictionary savedState)
        {
            base.Commit(savedState); 
            System.Diagnostics.Process.Start(Context.Parameters["TARGETDIR"].ToString() + "your main program.exe");
            //Remove temp files
            base.Dispose();
        }
    
        //Delete the file .InstallState (created when using custom actions in setup)
        //Could also delete this after the install (protected override void onAfterInstall) but not tested
        //Source: https://stackoverflow.com/questions/46786297/visual-studio-setup-project-remove-files-created-at-runtime-when-uninstall?rq=1
        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
            File.Delete(Context.Parameters["TARGETDIR"].ToString() + "your main program.InstallState");
            //Remove temp files
            base.Dispose();
        }
    }
}

Additional stuff for a Visual Studio Installer Projects (2017, 2019) that might save you a headache later:

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