Skip to content

VS2019 – 2022 Templates

Oleg Shilo edited this page May 2, 2024 · 5 revisions

Wix# comes with various VS2019/2022 project/item templates. They are all packed into Visual Studio Extension - WixSharp Project Templates, which can be downloaded from the Visual Studio Gallery or directly installed from the VS Extension manager.

Note, the latest version of VS project Templates is targeting new VS project specifications (like for .NET Core projects). The WixSharp nuget packages are designed to work in full with these projects.

The extension includes 5 project templates. The type of the project is reflected in the temple name. I.e. "WixSharp Managed Setup - Custom UI" is a template for the managed project WixSharp that has source code for all UI dialogs included in the Visual Studio project.

image

WiX Toolset installation

Wix# does require WiX binaries (compilers, linkers etc.). Wix# is capable of automatically finding WiX tools only if WiX Toolset is installed. In all other cases, you need to set the environment variable WIXSHARP_WIXDIR or WixSharp.Compiler.WixLocation to the valid path to the WiX binaries.

WiX binaries can be brought to the built environment by either installing WiX Toolset, downloading Wix# suite or by adding WixSharp.wix.bin NuGet package to your project. For bringing WiX Tools from NuGet use Install-Package WixSharp.wix.bin command.

MSI Authoring Steps

It is important to understand what is involved in building the MSI as otherwise it may be quite uneasy to troubleshoot the integration problems associated with the use of NuGet, VS and MSBuild.

Every Wix# VS project is a C# project that defines building an exe. This exe is an "MSI builder", which if executed uses WiX compilers to produce the final msi. Thus your program.cs file with static Main(...) is a build script that builds msi file. Interestingly enough, in the early releases of WixSharp (e.g. for WiX3) program.cs file was executed as a script, not as an application executable.

Thus if you just create manually a simple ConsoleApp project with the Wix# code (as below) and compile it it will build an exe but not msi.

class Script
{
    static public void Main()
    {
        var project = new Project("CustomActionTest",
                new ManagedAction("MyAction", Return.check, 
                                   When.After, Step.InstallInitialize, 
                                   Condition.NOT_Installed));

        project.BuildMsi();
    }
}

public class CustomActions
{
    [CustomAction]
    public static ActionResult MyAction(Session session)
    {
        MessageBox.Show("Hello World!");
        return ActionResult.Success;
    }
}

However, if you run the produced exe (e.g. F5) it in turn will build the desired msi. Instead of running the exe manually every time you can automate it by setting the project post-build event:
post-build event image

And the overall building process is below: image

All Wix# VS samples are composed this way.

Note, you don't have to set up post-build event manually if you use WixSharp Nuget package with Visual Studio. The package installation script does it automatically for you. All proper Wix# Visual Studio project templates also come with the post-build exe execution already scheduled. Though not via the post-build events but rather with *.targets, but that doesn't make any difference for the user.