WP7 : how to get the version number of my application ?

, , 13 Comments

It’s always a good thing to provide information to the users of your application. One thing which may seem trivial is the version number. In fact it’s a really important information because with it the customers can send you a feedback with enough information to know in which version you have to fix the bug.

In ConsoTracker, the app I build on Windows Phone, I forgot to provide it and users ask met to provide it !

In this post you’ll discover that the classic .Net way does not work and how to retrieve it from your code.

Warning !

Before I give you the code, be aware that the version number you set in your assembly is not read by the MarketPlace. In fact, you provide the information to it in the app submission process.

You do so have to update it in your code as you give the correct version to the market place.

How to do it ?

In .Net you can retrieve the version number of an application from it’s regarding assembly object instance.
[csharp]System.Reflection.Assembly.GetExecutingAssembly().GetName().Version[/csharp]
If you try it on a Windows Phone, you’ll discover that calling the property throw an exception. Also, as pointed out by Thomas in a tweet, it’s security critical which restricts it to internal use by the .NET Framework for Silverlight class library.

That’s too bad because it would have been clean way to retrieve it. Instead you have to read the assembly name and deduce the version number from it.

Here is the helper I use in my application:
[csharp]/// <summary>
/// Retrieve the current version of the executing assembly.
/// This is the one in the AssemblyVersion attribute.
/// </summary>
/// <returns></returns>
public static Version GetCurrentVersion()
{
Version version;

var assembly = Assembly.GetExecutingAssembly().FullName;
var fullVersionNumber = assembly.Split(‘=’)[1].Split(‘,’)[0];

version = new Version(fullVersionNumber);

return version;
}
[/csharp]

Be sure that the version number are synched if you put this code in another assembly. You can do this by sharing a file containing the AssemblyVersion attributes using ‘Add as link’ instead of adding a copy of the file.

Also, as it may represent a performance cost, I recommend you to to call it only once trough a static constructor.

 

13 Responses

  1. MBi

    07/11/2011 15 h 37 min

    I think the following is more accurate:

    AssemblyName assemblyName = new AssemblyName(Assembly.GetExecutingAssembly().FullName);

    string assemblyVersion = assemblyName.Version.ToString();

  2. MBi

    07/11/2011 15 h 42 min

    I'd rather avoid string operations:

    Version assemblyVersion = (new AssemblyName(Assembly.GetExecutingAssembly().FullName)).Version;

  3. southernsun

    07/11/2011 16 h 36 min

    you should also consider using State variables to store the initial result so you don't need to call this CPU extensive call again when you need the version number again. That's why the GetVersionFromManifestFile() is private and the GetVersion() is exposed to the user. This way it will try to get the Version number from the State variables and otherwise retrieve it from the manifest file.

  4. PaulW

    18/05/2012 18 h 08 min

    What would this look like in VB? I am having difficulty getting my code to recognize GetExecutingAssembly(), etc. I have an import for System.Reflection.

    Thanks…

Comments are closed.