I am using the Visual Studio Installer Projects extension for Visual Studio 2019. I have created my setup project and want to add custom actions which I want to implement as C# methods (I want the methods to install/uninstall a certificate). What I am not clear on is the linkage between the custom action configuration in the Custom Actions panel (appearing in Visual Studio) and the C# code I want to invoke. How do I create the class/methods such that they will be visible to the installer and activated as required? Does the class have to have a certain inheritance, and if so, what assemblies are required?
CodePudding user response:
You can create them just like you would a normal class. You will need to import a couple of namespaces to do it.
Specifically
- Microsoft.Deployment.WindowsInstaller;
- Microsoft.Tools.WindowsInstallerXml;
Here is a shortened version of a custom action I did for an installer a little while back.
using Microsoft.Deployment.WindowsInstaller;
using Microsoft.Tools.WindowsInstallerXml;
public class CustomActions : WixExtension
{
[CustomAction]
public static ActionResult CreateAndPopulateStorageFolder(Session session)
{
ActionResult result = ActionResult.Success;
try
{
session.Log("In Custom Action: CreateAndPopulateStorageFolder");
if (!Directory.Exists("C:\\Path"))
{
session.Log("In Custom Action: CreateAndPopulateStorageFolder: Directory does not exist");
Directory.CreateDirectory("C:\\Path");
Directory.CreateDirectory("C:\\Path");
Directory.CreateDirectory("C:\\Path");
}
}
catch (Exception ex)
{
session.Log(ex.Message);
result = ActionResult.Failure;
}
return result;
}
}
CodePudding user response:
I found the answer in this article. Basically amounts to adding an Installer
class to my assembly (inherits from System.Configuration.Install.Installer
) and overriding the appropriate methods. Need to set the InstallerClass property to True for the Custom Action Properties.
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
namespace InstallerCustomAction
{
[RunInstaller(true)]
public partial class MyInstaller : Installer
{
public MyInstaller()
{
InitializeComponent();
}
public override void Install(IDictionary stateSaver)
{
File.WriteAllText(@"C:\InstallTest.txt", "This is a test of an install action");
base.Install(stateSaver);
}
}
}
(Note that the template creates a corresponding Designer.cs file)
This doesn't seem to support incoming parameters, but it's not something I need.