Leo
Leo

Reputation: 5122

WIX: don't show build number from [ProductVersion]

My Wix installer has the product version set like this:

 <Product Version="4.6.0.0" ..>

And I show it in the UI like this:

 <String Id="WelcomeDlgTitle">{\WixUI_Font_Bigger}Welcome to the [ProductName] v[ProductVersion] Setup Wizard</String>

This works well but I would like to omit the build number (ie. v4.6.0 instead of v4.6.0.0). Is there a way to do this?

Upvotes: 2

Views: 1371

Answers (2)

Leo
Leo

Reputation: 5122

Thank you @zett42, your comment pointed me out in the right direction - I created a Wix Preprocessor Extension, following the documentation here: http://wixtoolset.org/documentation/manual/v3/wixdev/extensions/extension_development_preprocessor.html

Here are the classes I created:

public class MaterialiserExtension : WixExtension
{
    private MaterialiserPreprocessorExtension preprocessorExtension;

    public override PreprocessorExtension PreprocessorExtension
    {
        get
        {
            if (this.preprocessorExtension == null)
            {
                this.preprocessorExtension = new MaterialiserPreprocessorExtension();
            }

            return this.preprocessorExtension;
        }
    }
}

public class MaterialiserPreprocessorExtension : PreprocessorExtension
{
    private static string[] prefixes = { "versionUtil" };
    public override string[] Prefixes { get { return prefixes; } }

    public override string EvaluateFunction(string prefix, string function, string[] args)
    {
        string result = null;
        switch (prefix)
        {
            case "versionUtil":
                switch (function)
                {
                    case "ToString":
                        if (0 < args.Length)
                        {
                            result = Version.Parse(args[0]).ToString(args.Length >1 && args[1] != null ? int.Parse(args[1]) : 4);
                        }
                        else
                        {
                            result = String.Empty;
                        }
                        break;
                }
                break;
        }
        return result;
    }
}

One thing that was not immediately obvious - in my Visual Studio Wix project, I had to add -ext "$(SolutionDir)Materialiser.Wix\bin\Release\Materialiser.Wix.dll" to Properties (Alt+Enter) > Tool Settings > Compiler (candle) and the same in Linker (light).

To use, it is very simple: in my .wxs file I define a property like this:

<?define ProductVersion="4.6.0.5" ?>

Then I use it in Product like this:

<Product Version="$(var.ProductVersion)" .. >

And then I create anew property like this:

<Property Id="VersionWithNoBuildNumber" Value="$(versionUtil.ToString([ProductVersion], 3))" />

and in my .wxl file I use it normally:

<String Id="WelcomeDlgTitle">{\WixUI_Font_Bigger}Welcome to the [ProductName] v[VersionWithNoBuildNumber] Setup Wizard</String>

I hope this helps someone else too :p

Upvotes: 2

zett42
zett42

Reputation: 27756

You can omit the 4th field of the Product/@Version attribute as Windows Installer ignores it anyway.

<Product Version="4.6.0" ..>

If you really want to keep the 4th field of the version number, you'd have to write a custom action to parse the string and strip the 4th field.

Upvotes: 3

Related Questions