using System; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using WixSharp; using WixSharp.CommonTasks; using WixSharp.Controls; namespace WixSharpInstaller { class Program { static int Main() { try { // Validate input files var sourceBaseDir = GetInputFileDirectoryPath(); if (Directory.GetFiles(sourceBaseDir).Any() == false) throw new InvalidOperationException("The \"Files\" input directory is empty."); // Create project with important properties var project = new Project("c-entron Nexus"); project.GUID = new Guid("BBBE4001-00B1-41AC-B76F-CD2977F23A89"); project.SetVersionFromFile(Path.Combine(sourceBaseDir, "Files", "CentronNexus.Host.dll")); project.MajorUpgrade = new MajorUpgrade() // This is basically MajorUpgrade.Default but with AllowSameVersionUpgrades set to true { Schedule = new UpgradeSchedule?(UpgradeSchedule.afterInstallInitialize), DowngradeErrorMessage = "Eine neuere Version von [ProductName] ist bereits installiert.", AllowSameVersionUpgrades = true // This is needed, to force major upgrades when we change only the 4th number in the version (MSI by default only checks the first 3 numbers) }; project.SourceBaseDir = sourceBaseDir; project.ReinstallMode = "amus"; // Make sure to always re-install all files (see commit message for more details) project.Language = "de-DE"; project.ControlPanelInfo.Manufacturer = "NEXOWARE Systems GmbH"; // Add all files from the "Files" input directory project.AddDir(new Dir("%ProgramFiles%\\c-entron software gmbh\\c-entron Nexus", new Files("Files\\*.*"))); // Install CentronNexus.exe as windows-service project.ResolveWildCards().FindFirstFile("CentronNexus.Host.exe").ServiceInstaller = new ServiceInstaller { Name = "CentronNexus", DisplayName = "NEXOWARE ServiceBoard", Description = "Dieser Windows-Dienst startet NEXOWARE ServiceBoard, und damit alle Projekte die darin enthalten sind, wie z.B. das Web-Angebot oder das ServiceBoard.", // Make the service start automatically with windows Start = SvcStartType.auto, // The documentation states that "Install" is the default value, but it's actually not correct, same for StopOn and RemoveOn StartOn = SvcEvent.Install, StopOn = SvcEvent.InstallUninstall_Wait, RemoveOn = SvcEvent.Uninstall_Wait, }; // Declare UI project.UI = WUI.WixUI_InstallDir; project.RemoveDialogsBetween(NativeDialogs.WelcomeDlg, NativeDialogs.InstallDirDlg); // Remove the license-agreement dialog from the WixUI_InstallDir default UI, its between the WelcomeDlg and the InstallDirDlg project.BackgroundImage = "Images\\Installer-Background.bmp"; project.BannerImage = "Images\\Installer-Banner.bmp"; // Build MSI — set explicit output path to avoid ambiguity with relative working directory var outputDir = Path.GetDirectoryName(typeof(Program).Assembly.Location)!; project.OutFileName = Path.Combine(outputDir, "c-entron Nexus"); project.PreserveTempFiles = true; Compiler.BuildMsi(project); var msiPath = project.OutFileName + ".msi"; if (!System.IO.File.Exists(msiPath)) throw new FileNotFoundException($"MSI was not created at expected path: {msiPath}", msiPath); Console.WriteLine($"MSI successfully created at: {msiPath}"); return 0; } #pragma warning disable CA1031 catch (Exception e) #pragma warning restore CA1031 { Console.WriteLine(e); return 1; } } private static string GetInputFileDirectoryPath([CallerFilePath] string callerFilePath = null) { return Path.GetDirectoryName(callerFilePath); } } }