Introduction
Wix# is an open Source framework available on GitHub and used for generating Installer using C# syntax. Wix# is an open source framework for building installer setup for MSI source code by using script file that is written into C#. Wix# uses C# to produce a wix or MSI source code. Using wix#, easily create a sample installer MSI file if familiar with C# syntax. The logic behind wix# uses C# syntax that updates .wxs file and generates MSI source code.
Background
Wix installer can be compiled into two MSI packages - one for 32-bit and the other for 64-bit.Windows Installer’s inability to write to the 64-bit Program Files directory from a 32-bit MSI package.
Features
- Displays a license agreement
- Checks to prevent installing the 32-bit version on a 64-bit OS
- Uses the upgrade logic
- Custom dialog
- Puts an executable and a config file in %ProgramFiles%
- Prevents multiple installations of the same product, etc.
Using the Code
First, create a sample .NET C# application using Visual Studio.
Then, you need to add wix# package from nuget manager.
There are different ways to create installer with custom dialog box we can modify, but we can start with the first simple MSI file.
Check out the online HTML CheatSheet here and save this link because you might need it while composing content for a web page.
- using WixSharp;
-
- namespace WixSharpSetup
- {
- public class Program
- {
- private static void Main()
- {
- var project = new ManagedProject
- ("Sample.Installer",
- new Dir
- (@"%ProgramFiles%\Sample.Installer\Sample.Installer",
-
- new File("Program.cs")));
-
- project.GUID =
- new Guid("52e30b47-2514-4114-9095-1861ba258814");
-
- project.ManagedUI = ManagedUI.Empty;
- project.ManagedUI = ManagedUI.Default;
-
- project.ManagedUI = new ManagedUI();
- project.ManagedUI.InstallDialogs.Add(Dialogs.Welcome)
- .Add(Dialogs.Licence)
- .Add(Dialogs.SetupType)
- .Add(Dialogs.Features);
- project.Load += Msi_Load;
- }
- private static void Msi_Load(SetupEventArgs e)
- {
- if (!e.IsUISupressed && !e.IsUninstalling)
- MessageBox.Show(e.ToString(), "Load");
- }
- }
- }
Once you build your application, it will generate Sample.Installer.msi
setup.
When you build your application, this .msi setup file generates on basedirectory.
Later, when you start installing, it looks like below:
Check OS compatibility for many more features that would be available to build installer easily.
Generate Shortcut
- project.ResolveWildCards()
- .FindFile((f) =>
- f.Name.EndsWith("Sample.Installer.exe")).First()
- .Shortcuts = new[] {
- new FileShortcut
- ("Sample.Installer.exe", "INSTALLDIR"),
- new FileShortcut
- ("Sample.Installer.exe", "%Desktop%")
- };
-
-
-
-
-
-
-
-
-
-
-
-
Generate shortcuts of file "%Desktop%" generate shortcut on desktop.
Customize Licence Agreement
sing wix# also, you can specify your external .rtf license agreement file.
- project.LicenceFile = "D://licence.rtf"; // Relative path
-
-
-
Add Folders All Files into Installer .msi Setup File
- string path =
- @"E:\Projects\SampleInstaller\CodeBase\Devlopment\Sample.Installer\bin\Release\*.*";
- var project = new ManagedProject("Sample.Installer",
- new Dir(@"%ProgramFiles%\Sample\Sample.Installer"
- ,new Files(path)
- ));
Using Files class of Wix# library include folder all files. We can also specify condition for files we need to include into setup files for exa.
- string path =
- @"E:\Projects\SampleInstaller\CodeBase\Devlopment\Sample.Installer\bin\Release\*.*";
-
- Predicate<string> condition =
- new Predicate<string>(f => !f.EndsWith(".xml"));
-
- var data = new Files(path, condition);
-
- var project = new ManagedProject("Sample.Installer",
- new Dir(@"%ProgramFiles%\Sample\Sample.Installer"
- , new Files(path, condition)
- ));
Specified condition for including all files except a particular condition in the above code includes all files in Release folder excluding XML files.
Check for .NET Framework Version for Installer
- project.SetNetFxPrerequisite
- ("WIX_IS_NETFRAMEWORK_461_OR_LATER_INSTALLED >= '#394254'",
- "requires .NET Framework 4.6.1 or higher.");
-
-
-
-
-
-
-
-
Validate Assembly Compatibility
- private static void ValidateAssemblyCompatibility()
- {
- var assembly = System.Reflection.Assembly.GetExecutingAssembly();
-
- if (!assembly.ImageRuntimeVersion.StartsWith("v2."))
- {
- Console.WriteLine("Warning: assembly '{0}'
- is compiled for {1} runtime, which may not be compatible
- with the CLR version hosted by MSI. " +
- "The incompatibility is particularly
- possible for the EmbeddedUI scenarios. " +
- "The safest way to solve the problem
- is to compile the assembly for v3.5 Target Framework.",
- assembly.GetName().Name, assembly.ImageRuntimeVersion);
- }
- }
In wix#, also provide Event when your installer loads, before loading and after. It will work for when Installer starts installing, before that call RestApi and check for some valid key and if it's true
, only then will users allow install, otherwise not.
History
- First release of code and article
I have posted this article in CodeProject.com.