Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight 3 alternative to FileVersionInfo.GetVersionInfo

Within a Silverlight 3.0 application I want to use the AssemblyFileVersion to display the version information of the application. This is not the same as the AssemblyVersion and is typically retrieved in a .NET application using code such as:

var executingAssembly = Assembly.GetExecutingAssembly();
var fileVersionInfo = FileVersionInfo.GetVersionInfo(executingAssembly.Location);
var versionLabel = fileVersionInfo.FileVersion;

Unfortunately Silverlight 3.0 runtime does not include the FileVersionInfo class. Is there an alternative way to access this information?

like image 739
Martin Hollingsworth Avatar asked Jan 25 '10 06:01

Martin Hollingsworth


2 Answers

Here's a way to do it with attributes - I'm not sure if it will work in Silverlight though so you'll have to let me know.

Assembly assembly = Assembly.GetExecutingAssembly();
object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
    AssemblyFileVersionAttribute fileVersionAttribute = (AssemblyFileVersionAttribute)attributes[0];
    string version = fileVersionAttribute.Version;
}
like image 123
Sam Harwell Avatar answered Nov 18 '22 09:11

Sam Harwell


I found a solution to this in a twitter post by Craig Young (courtesy of Google's page caching) using Assembly.GetCustomAttributes as follows

var executingAssembly = Assembly.GetExecutingAssembly();
var customAttributes = executingAssembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);
if (customAttributes != null)
{
   var assemblyFileVersionAttribute = customAttributes[0] as AssemblyFileVersionAttribute;
   var fileVersionLabel = assemblyFileVersionAttribute.Version;
}

Posting this solution for future reference.

like image 4
Martin Hollingsworth Avatar answered Nov 18 '22 11:11

Martin Hollingsworth