Get Assembly version on windows phone 7

2020-06-09 10:20发布

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.

4条回答
等我变得足够好
2楼-- · 2020-06-09 10:59

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

查看更多
该账号已被封号
3楼-- · 2020-06-09 11:04

Try this:

    private static string GetVersionNumber()
    {
        var asm = Assembly.GetExecutingAssembly();
        var parts = asm.FullName.Split(',');
        return parts[1].Split('=')[1];
    }
查看更多
三岁会撩人
4楼-- · 2020-06-09 11:07

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);
    }
}
查看更多
兄弟一词,经得起流年.
5楼-- · 2020-06-09 11:21
 public static string GetVersion()
    {
        return Regex.Match(Assembly.GetExecutingAssembly().FullName, @"Version=(?<version>[\d\.]*)").Groups["version"].Value;
    }

is fairly clean as well.

查看更多
登录 后发表回答