Thanks to StackOverf开发者_Python百科low 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.
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...)
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:
- Right click on the main project (not the setup) -> Add -> Component -> Installer Class
- Name the Component and click Add
- Open the file click on switch to code view (or select ComponentName.cs -> ComponentName.Desinger.cs -> ComponentName -> ComponentName())
- Take a look at the code below, add the using statements, the commit and uninstall method. Run the code.
- Right click on the setup project -> View -> Custom Actions
- 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
- Repeat the above step for the Custom Actions Commit, Rollback and Uninstall
- Rebuild the setup
- Install the setup
- 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:
- Move the default installation from [ProgramFilesFolder] to [LocalAppDataFolder] so you don't have to ask for admin privileges every time a file is modified (View -> File System -> Properties of Application Folder -> Default location)
- Shortcuts are missing the full context menu
- Update removing some files especially shortcuts
- The Uninstaller may not remove all (.dll) files. The only fix I know of is to change the ProductName in the Setup
精彩评论