How to obtain the assembly version number in the Windows Store application.
This article is a personal blog backup article, the original address:
Http://validvoid.net/windows-store-app-get-assembly-version/
WinRT imposes many limitations on reflection. Assume that the Windows Store application references an assembly.MyApp.Utils
, One of the classes is calledMyUtils
, You can use the following method to obtain the AssemblyMyApp.Utils
.
Obtain the assembly version number
Method 1
using System.Reflection;public static string GetAssemblyVersion(){ return Assembly.Load(new AssemblyName("MyApp.Utils")) .GetName().Version.ToString();}
Note that method 1 has two constraints: one is to know the complete display name of the Assembly, and the other is that the Assembly output type must be a Class Library ). When the Assembly output type is Windows Runtime Component, method 1 returns the following error:
Could not load file or assembly 'MyApp.Utils, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
Method 2
using System.Reflection;public static string GetAssemblyVersion(){ return typeof(MyUtils).GetTypeInfo().Assembly.GetName().Version.ToString();}
Method 2 can be used when the Assembly output type is a class library or a Windows runtime component. The restriction is that you need to know a class in the Assembly and then passtypeof(MyUtils).GetTypeInfo().Assembly
Get the target assembly, and then get the assembly version number.
Obtain the Assembly file version number
using System.Reflection;public static string GetAssemblyFileVersion(){ return CustomAttributeExtensions.GetCustomAttribute( typeof(MyUtils).GetTypeInfo().Assembly).Version;}
CustomAttributeExtensions
Class contains static methods that can obtain custom features.AssemblyFileVersionAttribute
The value of the feature, that is, the file version number of the Assembly.
Availability
The above method is available in Windows/Windows Phone 8.1 Universal and Windows 10 UWP.
Reference
- CustomAttributeExtensions Class
- AssemblyFileVersionAttribute Class