Create Custom Action to Start Application and Exit Installer

╄→гoц情女王★ 提交于 2019-12-03 08:41:52

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!