How to display the app version on a Windows Phone page
It's not as simple as it may seem to display the current app version in a Windows Phone 7 app. Here you'll see how this can be solved.
Article Metadata
Code Example
Compatibility
Article
Necessary Steps
In a Windows Phone app there are at least two places where a version number is contained:
- The WMAppManifest.xml file and
- the AssemblyInfo of the main app assembly.
My solution to the problem involves reading out the contents of the WMAppManifest.xml file that's contained in every XAP package. If for whatever reason the manifest can't be open, the function will fall back to parsing the version from the AssemblyQualifiedName.
So what you'll have to do first is keeping the version number in those two places accurate.
AssemblyInfo.cs:
[assembly: AssemblyVersion("1.1.0.0")]
WMAppManifest.xml:
Next add a GetVersion() method to the App.xaml.cs code-behind file that reads out the version number:
public static string GetVersion()
{
Uri manifest = new Uri("WMAppManifest.xml", UriKind.Relative);
var si = Application.GetResourceStream(manifest);
// If we can open the WMAppManifest.xml file, parse it using XDocument and find the 'Version' attribute of the 'App' element.
if (si != null)
{
XDocument document = XDocument.Load(si.Stream);
XElement appNode = document.Descendants(XName.Get("App")).FirstOrDefault();
if (appNode != null)
{
XElement appElement = document.Element(XName.Get("App"));
if (appElement != null)
{
return appElement.Attribute(XName.Get("Version")).Value;
}
}
}
// If not, get the assembly version from the qualified name.
AssemblyName name = new AssemblyName(typeof(App).AssemblyQualifiedName);
return name.Version.ToString();
}
Then you can show the version number on a page by setting the text of a TextBlock, e.g. in the constructor of the page:
ApplicationTitle.Text += " Version " + App.GetVersion();
Sample Code
Sample code to this article is contained in the Media: ValueConverterSample.zip sample code for the Collection of Value converters for Windows Phone apps.




Contents
R2d2rigo - Isn't this a little overkill?
I think using Assembly.GetExecutingAssembly().GetName().Version is a better way of getting the current application version. And if you still want to do it parsing the WMAppManifest.xml, using XDocument (or even Regex) would be a better way than rolling out your own parser.r2d2rigo 13:54, 5 November 2012 (EET)
Influencer - Good point
R2D2rigo,
this is a good point. I'll try to improme the GetVersion method and discuss the assembly version variant. Please give me some time, as I'm rather busy currently.
Thanks,
Thomasinfluencer 09:02, 6 November 2012 (EET)
R2d2rigo -
Hello influencer,
If you are rather busy I can modify the article and the source code later in the day.
Cheers.r2d2rigo 11:32, 6 November 2012 (EET)
R2d2rigo -
I have modified the article and uploaded a revised version of the code.r2d2rigo 20:21, 6 November 2012 (EET)
Influencer - Well done
Thanks for your help!
Thomasinfluencer 22:56, 6 November 2012 (EET)
Hamishwillee - Wow
It is often hard to let someone else help you with an article. It is often easier to ignore a problem in someone elses article that sort it out. Professionally handled guys. Thank you.hamishwillee 07:51, 7 November 2012 (EET)
Influencer - That's what a Wiki is for...
Isn't it?influencer 16:43, 7 November 2012 (EET)
Hamishwillee - Yes it is
But you'd be surprised how many people don't use it correctly.hamishwillee 06:16, 8 November 2012 (EET)