【问题标题】:Create Custom Action to Start Application and Exit Installer创建自定义操作以启动应用程序和退出安装程序
【发布时间】:2011-03-11 11:41:18
【问题描述】:

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

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

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

【问题讨论】:

标签: visual-studio-2010 installation windows-installer custom-action


【解决方案1】:

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

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

按照以下步骤操作:

  1. 右键主项目(不是设置)->添加->组件->安装程序类
  2. 为组件命名并单击添加
  3. 点击打开文件切换到代码视图(或选择ComponentName.cs -> ComponentName.Desinger.cs -> ComponentName -> ComponentName())李>
  4. 看看下面的代码,添加 using 语句,提交和卸载方法。运行代码。
  5. 右键点击设置项目->查看->自定义操作
  6. 右键单击安装 -> 添加自定义操作... -> 应用程序文件夹 -> yourProject 的主要输出(活动) -> 确定 -> 右键单击​​添加的文件并选择属性窗口(或 F4) -> 将 CustomActionData 设置为/TARGETDIR="[TARGETDIR]\"(末尾有一个反斜杠)并确保 Installer Class 设置为 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 年)的其他内容可能会让您以后头疼:

【讨论】:

    【解决方案2】:

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

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

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

    真的很重要。只是把这个留给未来的访问者(如果允许我的话,我会添加评论......)

    【讨论】:

      【解决方案3】:

      经过一番谷歌搜索后,我发现 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]\"
      

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

      正是我需要的。感谢 Google 拯救了我的培根。

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

      想出了如何摆脱临时文件。使用 Dispose() 更新代码。

      【讨论】:

      • 当您说“添加 > 项目输出并添加安装程序类”时,您是什么意思?单击“添加”>“项目输出”后,出现“添加项目输出组”对话框。你选择了什么?初级输出?源文件?您究竟是如何添加 Installer 类的?
      • @DanCsharpster InstallProject > 右键单击​​ > 添加项目输出 > 从下拉列表中选择项目 > 选择主要输出 > 确定。查看 | CustomActions > 右键单击​​安装 > 添加自定义操作 > 双击应用程序文件夹 > 选择“来自 {CustomActionprojectname} (Active) 的主要输出”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      相关资源
      最近更新 更多