Skip to content

Building MSI – Step by step tutorial

Oleg Shilo edited this page Jan 15, 2022 · 9 revisions

WixSharp Installation

Building MSI with Wix# is quite simple. But first, you need prepare your building environment:

  • [Optional step] Download (and extract) Wix# binaries from the Releases section. You may but don’t have to download WiX as Wix# package already contains a compatible copy of WiX. You only need this step if you are not going to use Visual Studio or MSBuild.
  • If you don't have WiX installed, create environment variable WIXSHARP_WIXDIR pointing to the WiX ‘bin’ folder
    • you can do it either manually (normally the ‘bin’ path is <WixSharp>\Wix_bin\bin)
    • or you can just execute install.cmd from the Wix# package You will need to ensure that WIXSHARP_WIXDIR was properly created otherwise Wix# may not be able to find WiX compilers.

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 build 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.

Now you can build your MSIs. 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