Getting the assembly version in a Windows Store App

Getting the version number of the executing assembly can be really useful, either to display to user in the about screen, or include in crash reports and feedback forms. Every incarnation of the framework seems to have a different way of doing this and .NET for Windows Store Apps is no exception.

You can get the version number from an instance of the Assembly class; Unfortunately the Assembly.GetExecutingAssembly()method isn't available, so you have to get a reference to the executing assembly in a different way. In Windows Store Apps you can access the current assembly by looking at the type information for any type in that assembly.

Most members of System.Type have now been moved into the System.Reflection.TypeInfo class, which you can get via the Type.GetTypeInfo() extension method. (Note: this extension method exists in the System.Reflection namespace, so you'll need to make sure its in your using list to get access to it.)

From the obtained TypeInfo you can access the Assembly property, and from that the version number:

 var assemblyVersion = this.GetType().GetTypeInfo().Assembly.GetName().Version.ToString(4);

It can also be useful to get the value of the AssemblyFileVersion attribute (often this version number is used rather than the assembly version as it doesn't cause the AssemblyQualifiedName to change). You can access any attribute on the Assembly by calling the Assembly.GetCustomAttribute<T> method, where T is the attribute you want to access:

 var fileAssemblyVersion = this.GetType().GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;

Using these two snippets you can now access both the Assembly and Assembly File versions of your Windows Store App, allowing you to uniquely identify the version of the app your crash reports and feedback relate to.

Chris