23

In C# one can use System.Version.Assembly to get the version of a running app. However this doesn't appear to exist in Silverlight for Windows Phone. Is there an alternative?

3
  • @AnthonyWJones OK, now I understand your comment. You're saying henry hasn't accepted an answer for the questions he asks. Oct 1, 2010 at 2:17
  • i think some newbies don't figure out that they can accept an answer as correct... Oct 1, 2010 at 16:32
  • 1
    The answer works for silverlight but does not work on Windows Phone where version is not exposed. Instead, the solution is: String appVersion = System.Reflection.Assembly.GetExecutingAssembly().FullName.Split('=')[1].Split(',')[0];
    – henry
    Oct 13, 2010 at 19:20

11 Answers 11

38

You can use the GetExecutingAssembly method and the AssemblyName class to find this information.


  var nameHelper = new AssemblyName(Assembly.GetExecutingAssembly().FullName);

  var version = nameHelper.Version;
  var full = nameHelper.FullName;
  var name = nameHelper.Name;

1
  • 1
    Will it bring the version we set while uploading the xap in market?
    – Mubashar
    Apr 3, 2014 at 12:16
19

If you have moved over to Windows Phone 8, you can simply use the newer PackageId class:

var version = Package.Current.Id.Version;
1
  • simple and effective..Thnx
    – Kartiikeya
    Nov 25, 2015 at 9:59
18

I don't how @henry accepted the answer because all answers are talking about Dll version but when one is talking about getting the version of windows phone app that means version of app on the market. I don't know about others but I really don't care about the version of dll and also I use market version to label the source in source control.

When a developer upload XAP on the market he/she specifies the version of XAP which can be different then the dll version, while processing Market reads information from WMAppManifest.xml file and write backs the version you specify on the XAP submission page.

So the desired version is available in WMappManifest.xml file which you can read by XmlReader like following;

    public static string GetAppVersion()
    {
        var xmlReaderSettings = new XmlReaderSettings
        {
            XmlResolver = new XmlXapResolver()
        };

        using (var xmlReader = XmlReader.Create("WMAppManifest.xml", xmlReaderSettings))
        {
            xmlReader.ReadToDescendant("App");

            return xmlReader.GetAttribute("Version");
        }
    }

Here is sample WMAppManifest.xml

<Deployment xmlns="http://schemas.microsoft.com/windowsphone/2012/deployment" AppPlatformVersion="8.0">
  <DefaultLanguage xmlns="" code="en-US"/>
  <App xmlns="" ProductID="{cc18507d-0de0-42d6-8b0f-05addeafd21e}" Title="CaledosLab.Phone.ContosoLogTest" RuntimeType="Silverlight" Version="1.0.0.0" Genre="apps.normal"  Author="CaledosLab.Phone.ContosoLogTest author" Description="Sample description" Publisher="CaledosLab.Phone.ContosoLogTest" PublisherID="{a204adfc-7718-4c4a-afb4-c1c39ec50d30}">
  </App>
</Deployment>

So you can read whatever information you want from App xml tag the same way as we read version from app tag. e.g. publisher Id or Product Id

2
  • 1
    Why not use the Package class as Magnus's answer below? Sep 11, 2015 at 22:25
  • 1
    @JamieKitson: question was for windows-phone-7 and it was the time when people consider to support of both platforms
    – Mubashar
    Sep 14, 2015 at 0:40
10

On Phone 7 there is no clean way to get the version. The best thing to do is parse the Full Name (which is the only exposed property) for the version string:

String appVersion = System.Reflection.Assembly.GetExecutingAssembly()
                    .FullName.Split('=')[1].Split(',')[0];
5

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);
    }
}
3

To get App Version from "WMappManifest.xml", this solution might be more efficient than Mubashar Ahmad solution, but it will only work for WP8+, not WP7:

using (var stream = new FileStream("WMAppManifest.xml", FileMode.Open, FileAccess.Read))
{
    var appVersion = XElement.Load(stream).Element("App").Attribute("Version");
    return appVersion != null ? appVersion.Value : null;
}
3
  • 1
    i couldn't help myself, I compared your solution to Mubashar Ahmad using a Stopwatch, and I found that your solution did it in less than half the number of elapsed ticks :) Aug 27, 2014 at 10:43
  • Could just have been coincidental, but I'll take less lines of code any day. Aug 27, 2014 at 10:52
  • 1
    @JesperRiemerAndersen In the mean time, I've further improved my code over the time. I just made an edit to reflect my latest optimization. Yes, I did use Stopwatch to compare over 4 different xml parsing of this file.
    – Cœur
    Aug 27, 2014 at 11:52
2
string versionApp = XDocument.Load("WMAppManifest.xml").Root.Element("App").Attribute("Version").Value;
1
System.Text.RegularExpressions.Regex regexVersion = new System.Text.RegularExpressions.Regex(@".*(?<v>\d+.\d+.\d+.\d+).*");
System.Text.RegularExpressions.Match regexVersion_Match = regexVersion.Match(System.Reflection.Assembly.GetExecutingAssembly().FullName);
string appVersion = "";
if (regexVersion_Match.Success)
    appVersion = regexVersion_Match.Groups["v"].Value;
1
public static string GetAsemblyVersion()
{
    return Convert.ToString(Assembly.GetCallingAssembly().GetName().Version);
}
1

Simply use this line to get the Application Name and Id, publisher name etc...

string name = Windows.ApplicationModel.Package.Current.DisplayName;
0

Windows Phone 8.1:

using System.Reflection;

// ...

Version version = typeof(MainPage).GetTypeInfo().Assembly.GetName().Version;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.