12

In my c# applications I usually get the version (to show the customer) using the following code:

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version

This does not work in Windows Phone 7 (it hangs the emulator, and phone crashing is a no-no for MS).

So, how do I get the version of the executing on a windows phone 7 device??

[Update] as noted in the comments below, calling GetName() in a wp7 app seems to be the problem.

Sam
  • 28,421
  • 49
  • 167
  • 247
  • I would definitely report this to Microsoft. Your code is correct, and the hanging is definitely caused by a bug either in the .NET in the emulator, or in the whole .NET for WP7!! – usr-local-ΕΨΗΕΛΩΝ Nov 11 '10 at 09:50

4 Answers4

22

Try this:

    private static string GetVersionNumber()
    {
        var asm = Assembly.GetExecutingAssembly();
        var parts = asm.FullName.Split(',');
        return parts[1].Split('=')[1];
    }
Matt Lacey
  • 65,560
  • 11
  • 91
  • 143
  • Well, this is essentially the same I wrote above, and it hangs the phone just like my version does. – Sam Nov 11 '10 at 10:37
  • 1
    @Sam this is working for me in apps in the Marketplace. I had to parse the version number out of the FullName as `Version` wasn't working for me. What error are you getting? – Matt Lacey Nov 11 '10 at 10:43
  • Aah, I checked back, I did not use FullName, but GetName instead. And whenever I have a call to GetName in my app, the emulator, device and debugger just hang - not when the GetName is hit, but when the app is started! – Sam Nov 11 '10 at 14:32
  • So even when GetName is not called, just its existence in the code is enough to crash the program. #Strange – Sam Nov 11 '10 at 14:32
  • A post on it : http://www.jonathanantoine.com/2011/11/07/wp7-how-to-get-the-version-number-of-my-application/ – Jonathan ANTOINE Nov 07 '11 at 10:48
  • `GetName()` is marked as `SecurityCritical` while `FullName` isn't. – Agent_L Dec 07 '12 at 17:30
2

Does parsing it out of

Assembly.GetExecutingAssembly().FullName

work for you?

example output: SomeApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

edit: don't need to go through ManifestModule

Mick N
  • 14,892
  • 2
  • 35
  • 41
  • I don't even get that far: it seems use of reflection kills the phone. – Sam Nov 11 '10 at 10:37
  • Testing on Samsung Taylor prototype device System.Diagnostics.Debug.WriteLine(Assembly.GetExecutingAssembly().FullName);... no probs. – Mick N Nov 11 '10 at 10:48
  • Yeah, you were right, I mistakenly used GetName instead of FullName. – Sam Nov 11 '10 at 14:33
1
 public static string GetVersion()
    {
        return Regex.Match(Assembly.GetExecutingAssembly().FullName, @"Version=(?<version>[\d\.]*)").Groups["version"].Value;
    }

is fairly clean as well.

1

First, I think it's more apt to use the assembly's file version info for conveying the application version to the user. See http://techblog.ranjanbanerji.com/post/2008/06/26/Net-Assembly-Vs-File-Versions.aspx

Second, what about doing this:

using System;
using System.Linq;
using System.Reflection;

public static class AssemblyExtensions
{
    public static Version GetFileVersion(this Assembly assembly)
    {
        var versionString = assembly.GetCustomAttributes(false)
            .OfType<AssemblyFileVersionAttribute>()
            .First()
            .Version;

        return Version.Parse(versionString);
    }
}
  • Problem there is that it returns the particular string set in AssemblyInfo.cs, which means if you wildcard revision and build, you get something like "1.2.*" instead of "1.2.3.4" – Chris Charabaruk Apr 04 '12 at 02:48
  • According to MSDN (http://msdn.microsoft.com/en-us/library/system.reflection.assemblyfileversionattribute.assemblyfileversionattribute.aspx) wildcards are not supported for AssemblyFileVersionAttribute, and doing so will emit a compiler warning because it's meaningless in this context. Use of wildcards for the version is only applicable to AssemblyVersion (http://msdn.microsoft.com/en-us/library/system.reflection.assemblyversionattribute.assemblyversionattribute.aspx) – Rafael Dowling Goodman Jul 11 '12 at 01:09