问题
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.
回答1:
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().
回答2:
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...)
来源:https://stackoverflow.com/questions/3172406/create-custom-action-to-start-application-and-exit-installer