C# – Running custom action before installation

ccustom-actionwindows-installer

I have a basic setup project in Visual Studio 2010, and I am struggling with setting up custom actions. I have 3 Installer classes in a separate assembly.

The 3 classes do the following things:

  • Check for parameters to allow for unattended installation with licensing information (overrides the Install method)
  • Remove a previous installation of the software (installed with an NSIS installer, uses the BeforeInstall event)
  • Stop the program if it is running, using the BeforeInstall event

The problem is the last one, it seems the BeforeInstall event is never triggered. I've tried to make it create a file on disk, and displaying a MessageBox. It never triggers.

The "Primary output from SetupActions" has been added to the "Install" section in the Custom Actions editor. My Installer classes all have [RunInstaller(true)].

Code that does not work:

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

    private void InstallerStopProgram_BeforeInstall(object sender, InstallEventArgs e)
    {
        MessageBox.Show("This is never displayed during the install");
    }
}

Best Answer

Problem with this approach is that you want to run something before installation and that something resides in the dll/exe that gets onto the target machine only as a part of installation. So unless installation is done, you will not have your BeforeInstall code on the disk to run (and then you can make it run only as post install).

To my knowledge, what you want to achieve is not possible VS 2010 setup project. Perhaps, you should use Wix (or NSIS)!

Related Topic