Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Action not working - Visual Studio Setup Project

In the past we have used Advanced Installer to build our .msi installers for a particular project. Our yearly license for Advanced Installer has expired, so to avoid the renewal cost, and because I think the same can be accomplished with Visual Studio, I am attempting to use a Visual Studio 2010 Setup Project to build my .msi.

For the most part, the installer I have built with Visual Studio works fine. However, one thing we need the installer to do is run a couple of .reg files to add a large collection of settings to the registry (It may be worth noting that this is old software that is only being maintained and updated until it is replaced entirely in the near future. It is not practical to change our method of storing settings). With Advanced Installer, we were able to execute a .cmd file as an "Install" Custom Action that would run these .reg files that were also included in the installation. VS Setup Projects have Custom Actions, but it appears that here they are required to be either .dll or .exe files, so I must find an alternative to using a .bat or .cmd file.

First, I tried adding a Command Line project to my solution that consisted only of the following lines in the main() method:

using (Process registryInput = Process.Start("regedit.exe", "/s Settings1.reg"))
{
    registryInput.WaitForExit();
}

using (Process registryInput= Process.Start("regedit.exe", "/s Settings2.reg"))
{
    registryInput.WaitForExit();
} 

I added the Primary Output of this project to the "Install" folder of the "Custom Actions" editor. Tried to run the installer, but the command line process never seemed to run and no registry settings were installed. If I manually ran the command line executable from the application directory where it was installed, it added the registry entries as intended - so the problem is not with the code I'm using to call the .reg files.

I turned to MSDN and changed my solution to be modeled after their Custom Actions Walkthrough. I created a Class Library project (and removed my Command Line project) and added an Installer Class. Instead of starting up a browser using Microsoft's website URL in the Commit() method as shown in their example, I added the code above to the Install() method. Here is what I ended up with:

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

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Install(IDictionary stateSaver)
    {
        base.Install(stateSaver);

        using (Process registryInput = Process.Start("regedit.exe", "/s Settings1.reg"))
        {
            registryInput.WaitForExit();
        }

        using (Process registryInput = Process.Start("regedit.exe", "/s Settings2.reg"))
        {
            registryInput.WaitForExit();
        } 
    }

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Commit(IDictionary savedState)
    {
        base.Commit(savedState);
    }

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Rollback(IDictionary savedState)
    {
        base.Rollback(savedState);
    }

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Uninstall(IDictionary savedState)
    {
        base.Uninstall(savedState);
    }
}

I added the Primary Output of this new Class Library project to the "Install" folder of the "Custom Actions" editor. Still, when I run the installer, the code does not appear to be executed and my registry settings are not added. I have tried this installer both set to "Install for all users" and "This user only".

Any help to either get this Custom Action working or an alternative method to get a .reg file to run on install will be greatly appreciated. Thank you in advance.

like image 848
Tim Coolman Avatar asked Jun 01 '11 17:06

Tim Coolman


People also ask

How do I add a custom action to Visual Studio setup project?

To create the custom action, open the gadget project in Visual Studio. On the Project menu, select Add Class, select Installer Class in the Add New Item dialog box. Accept the default name of Installer1. cs, and then click Add.

How do I add installer class to setup project?

To add a custom action click on the custom action icon. This will open the following window and there we can add a project containing an installer class. To create this project having an installer class, we can make a class library type of project and add an installer class in it from the add new item dialog.

What is MSI custom action?

The custom actions are the actions that can be performed together with the MSI package install and/or uninstall process. They can be used, for example, to prepare the system for installation of the package, to check prerequisites, to start the application on installation completion, etc.


1 Answers

I just ran across this same issue, re: the custom action not being picked up by the installer. The resolution was to run Visual Studio as an administrator.

Even though I'm a full admin on my machine without any restrictions (AFAIK), the installer would never pick up the custom actions. As soon as I closed down Visual Studio and then restarted as an administrator (right click > run as administrator), the custom actions were immediately picked up by the installer.

like image 133
Metro Smurf Avatar answered Oct 06 '22 08:10

Metro Smurf