WPF Version Numbers

This post examines a couple of different ways that WPF version numbers can be stored and retrieved.

It's important to display the version of your WPF application somewhere in the user interface, but before you can display it, you need to know how to store and retrieve it. There are at least a couple of ways this can be accomplished and that's what we'll examine in this post.

Version Property

The most straightforward and popular approach is simply to use the Version property from the assembly's name. This can be set by right-clicking on the project and setting the Assembly version within the project's properties. In a .NET Framework app, this is found under the Application -> Assembly Information section of the project's properties, while in .NET Core, it can be found in the Package section of the project's properties.

Here's how you can read the value in code.

// Retrieve the assembly that contains this code.
// If this code lives in a DLL, you might want to use
// Assembly.GetEntryAssembly() instead.
var assembly = Assembly.GetExecutingAssembly();
// Get the version from the assembly.
var version = assembly.GetName().Version.ToString();
How to read the Version property from an assembly

AssemblyInformationalVersionAttribute

Sometimes you might want to describe your version as a string instead of a series of octets. For example, maybe your version is "2.0.0-alpha.3". If you want to describe your version in this way, then you won't be able to use the Version property mentioned above because it limits you to four integer octets, like "3.0.1.0".

The AssemblyInformationalVersionAttribute can be used to specify a string with version info. This will display a warning if the string is not in the format that is used by the assembly's version number, or if the string contains wildcard characters, but the warning can be ignored.

// Retrieve the assembly that contains this code.
// If this code lives in a DLL, you might want to use
// Assembly.GetEntryAssembly() instead.
var assembly = Assembly.GetExecutingAssembly();
// Search the assembly attributes for the informational
// version number and return the first one found or
// null if none were found for this assembly.
var version = assembly
    .GetCustomAttributes<AssemblyInformationalVersionAttribute>()
    .Select(x => x.InformationalVersion)
    .FirstOrDefault();
How to read AssemblyInformationalVersionAttribute

.NET Framework

If your application runs on the traditional .NET Framework, then you'll want to ensure that you have the attribute added to the AssemblyInfo.cs file.

using System.Reflection;

[assembly: AssemblyInformationalVersion("1.0.0-alpha.4")]
AssemblyInformationalVersion in AssemblyInfo.cs of .NET Framework project

.NET Core

If you're using .NET Core, the AssemblyInformationalVersionAttribute is automatically added and is based on the <Version></Version> element in the csproj file.

<PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWPF>true</UseWPF>
    <LangVersion>8.0</LangVersion>
    <Nullable>enable</Nullable>
    <!-- This populates the AssemblyInformationalVersionAttribute -->
    <Version>1.0.0-alpha.4</Version>
</PropertyGroup>
AssemblyInformationalVersion being derived from csproj in .NET Core project

If your application still explicitly defines an AssemblyInformationalVersionAttribute inside of your .NET Core project, then you'll get an error that states "Duplicate 'System.Reflection.AssemblyInformationalVersionAttribute' attribute". The easy way to resolve this is just to delete the attribute from your code (typically located in AssemblyInfo.cs) and just use the <Version></Version> xml element inside of your csproj instead.