Create Custom Action to Start Application and Exit Installer

前端 未结 2 1764
感情败类
感情败类 2021-02-10 20:57

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 th

相关标签:
2条回答
  • 2021-02-10 21:13

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

    0 讨论(0)
  • 2021-02-10 21:26

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

    0 讨论(0)
提交回复
热议问题