A simple WIX installer that runs custom actions on install and uninstall.

Below I provided the source code of WIX installer that shows the license, installation directory and runs custom actions on install and uninstall:

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Product Id="*" Name="My Game" Language="1033" Version="1.0.0.0" Manufacturer="SharLines Corporation" UpgradeCode="...">
        ...    

        <Property Id="WIXUI_INSTALLDIR" Value="INSTALLFOLDER" ></Property>
        <WixVariable Id="WixUILicenseRtf" Overridable="yes" Value="License.rtf"/>
        <UIRef Id="WixUI_InstallDir"/>

        <InstallExecuteSequence>
            <Custom Action="InstallService" After="InstallFiles">(NOT Installed) AND (NOT REMOVE)</Custom>
            <Custom Action="UninstallService" After="InstallInitialize">(NOT UPGRADINGPRODUCTCODE) AND (REMOVE="ALL")</Custom>
        </InstallExecuteSequence>
        <CustomAction Id="InstallService" Return="check" Impersonate="yes" Execute="deferred" Directory="INSTALLFOLDER" ExeCommand="[INSTALLFOLDER]$(var.MyService.TargetFileName) parameters..."/>
        <CustomAction Id="UninstallService" Return="check" Impersonate="yes" Execute="deferred" Directory="INSTALLFOLDER" ExeCommand="[INSTALLFOLDER]$(var.MyService.TargetFileName) parameters..."/>
    </Product>
    ...
</Wix>

MyService is project referenced by my wixproj in VS2015. If Impersonate=”yes” the command is run as the current user, if “no”, the command is run as “NT AUTHORITY\SYSTEM“. INSTALLFOLDER is defined as follows:

<Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
        <Directory Id="ProgramFilesFolder">
            <Directory Id="INSTALLFOLDER" Name="PrmServerSetup" />
        </Directory>
    </Directory>
</Fragment>

Also there is a typical fragment that copies the files:

<Fragment>
    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
        <Component Id="ProductComponent">
            <File Id="MyService" Name="$(var.MyService.TargetFileName)" Source="$(var.MyService.TargetPath)" DiskId="1" />
        </Component>
        ...
    </ComponentGroup>
</Fragment>

I used the following C# code to determine what user the action is run under:

class Program
{
    static int Main(string[] args)
    {
        string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

        using (StreamWriter writer = new StreamWriter(@"C:\work\SetupLog.txt"))
        {
            writer.WriteLine("User: {0}", userName);
        }

        return 0;
    }
}

Leave a Reply

Your email address will not be published.