Skip to content

Building MSI – Step by step tutorial

Oleg Shilo edited this page Jun 9, 2024 · 10 revisions

WixSharp Installation

This page is an overview of the project setup steps and the tooling options. It's important that you decide which version of WiX Toolset you want to use. It's important as WiX has two product streams that require very different build approaches. Thus building msi setup files with WixSharp requires different build steps for preparing your build environment.

Building MSI with Wix# is quite simple. But first, you need to prepare your building environment. And preparing it for WiX3 and WiX4+ is different:

Installing WiX4+ tools

Install WiX as a .NET Tool:

dotnet tool install --global wix

Installing WiX3 tools

WixSharp is capable of automatically finding WiX3 tools but only if WiX Toolset is installed. In case if you just downloaded and extracted WiX tools manually, 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 build environment by either installing WiX Toolset or by adding WixSharp.wix.bin NuGet package to your project. For bringing WiX Tools from NuGet use Install-Package WixSharp.wix.bin command.

Creating the .NET Project

  1. Install Visual Studio WixSharp Templates (read more here)
  2. Create your project from one of the WixSharp templates and you will be able to build the project right away.

Building your product setup

The project template comes with the program.cs file containing the definition of a sample setup. You will need to modify this file to implement your own deployment logic for your product.

As an example let’s create a simple MSI for installing MyApp.exe and readme.txt files.

Target destination:  
    [Program Files]\My Company\My Product

Files to deploy:
    <root>\Files\Docs\readme.txt<br>
    <root>\Files\Bin\MyApp.exe

Step 1
In the root folder create the build script setup.cs file (of course you can use canonical program.cs instead) containing the following code:

using System;
using WixSharp;
  
class Script 
{
    static public void Main()
    {
        var project = new Project("MyProduct",
                          new Dir(@"%ProgramFiles%\My Company\My Product",
                              new File(@"Files\Docs\readme.txt"),
                              new File(@"Files\Bin\MyApp.exe")));
 
        project.GUID = new Guid("6f330b47-2577-43ad-9095-1861ba25889b");
 
        Compiler.BuildMsi(project);
    }
} 

Step 2
Now you need to execute setup.cs script. It can be bone using one of the following techniques:

  • Building with CS-Script

    • Place both cscs.exe and WixSharp.dll from WixSharp package in the directory.
    • Run “cscs.exe setup.cs” from command prompt or batch file Image
  • Building with Notepad++

    • Place WixSharp.dll from WixSharp package in the root directory.
      Alternatively you can instruct Notepad++ to download the dll from the NuGet server by placing //css_nuget directive at the top of your script:

      //css_nuget WixSharp.bin;
      //css_ref System.Core.dll;
      using System;
      using WixSharp;
      ...
    • Load setup.cs in Notepad++ and press F5. image

  • Building with Visual Studio

Automated:

  • Install WixSharp templates (VS2019-2022) from the Visual Studio extension management dialog.
  • Now create the project from WixSharp template, update the content of program.cs and build the project as any other C# project. Find more info here.

Manual:

  • In the root directory create C# Console Application project. Ensure project file and Program.cs are located in the root directory.
  • Use package manager to install NuGet package: WixSharp.
  • If In the project properties ensure ‘Target framework’ is set to v4.5.1. Strictly speaking you can set it to any version higher. "4.5.1" is arguably the safest choice as this version most likely is present on all deployment targets.
  • Build/Rebuild project