Wednesday, December 29, 2010

Application Version in Windows Phone 7

The typical method of retrieving application version using the code shown below doest work in Windows Phone 7 (it throws an exception).

string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();

Instead you need to use the following code:

public static string GetVersion()
{
const string VerLabel = "Version=";
string assemblyName = System.Reflection.Assembly.GetExecutingAssembly().FullName;
int startIndex = assemblyName.IndexOf(VerLabel) + VerLabel.Length;
int endIndex = assemblyName.IndexOf(',', startIndex+1);
string version = assemblyName.Substring(startIndex, endIndex - startIndex);
return version;
}

1 comment:

Vangos said...

Thank you very much! You saved my day.