C# Setup Project Custom Action to save the installation directory to custom setting file

Thodor12 picture Thodor12 · Jul 13, 2016 · Viewed 9.5k times · Source

I am trying to make an application using windows installer as it's install method. When the installation succeeds I want to get the path where the primary output is located that I configured in the installer. In my case the primary output is in the folder [ApplicationData(Installroot)]\Bin\.

I also have a custom settings file called App.Settings in 1 my Class Library which controls several settings like file locations who are relative to the install location.

So the idea is that when the installation succeeds it should call the App.Settings and save the install folder to the settings file.

I already made an Install class and put it inside the Class Library. I am not sure if it is supposed to be in that project though. This is the code of the Install Class:

using System.Collections;
using System.ComponentModel;

namespace WaspbaneModels
{
    [RunInstaller(true)]
    public partial class Installer : System.Configuration.Install.Installer
    {
        public Installer()
        {
            InitializeComponent();
        }

        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
        }

        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);

            SettingsControl.BaseURL = Context.Parameters["assemblypath"];
            SettingsControl.Save();
        }

        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
        }

        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
        }
    }
}

In this code SettingsControl is the class that takes care of the Settings simply with some properties. I used this class so that my Windows Forms project also can access these settings.

In the Custom Actions tab I also added the Primary Output to everything. I am also not sure if this is correct because I think the Primary Output contains all .dll files including the Class Libraries.

I am not really sure where to continue from this point however because the Settings are not saved. I simply check this by putting up a messagebox at the startup of the application giving me the value of the setting.

Anyone interested in more data of the project just ask. I just won't post everything at once yet.

EDIT:

After some more testing I managed to find out that the Installer class isn't being called upon. I added something that would write out to a file when the method would've been called but nothing happened.

Therefore my question now is: How do I add the Custom Action to the Setup project properly?

Answer

Yoruba picture Yoruba · Feb 24, 2017

You can add the custom action class (.dll) to the Setup project. You have to add the InstallerActions.dll to the Install and Commit actions.

enter image description here

To add the custom action

  1. Select the Setup Installer project in Solution Explorer. On the View menu, point to Editor, and then click Custom Actions. The Custom Actions Editor is displayed.
  2. In the Custom Actions Editor, select the Commit node. On the Action menu, click Add Custom Action.
  3. In the Select Item in Project dialog box, double-click the Application Folder. Select Primary output from InstallerActions project. Primary output from InstallerActions appears under the Commit node in the Custom Actions Editor.
  4. In the Properties window, make sure that the InstallerClass property is set to True (this is the default).
  5. In the Custom Actions Editor, select the Install node and add Primary output from InstallerActions to this node as you did for the Commit node.