5

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?

Martin Hollingsworth
  • 7,249
  • 7
  • 49
  • 51

2 Answers2

5

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;
}
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • It does work in Silverlight 3. I already had the answer and was posting if for future reference. My answer attributes the solution to where I found the info first. Thanks anyway. – Martin Hollingsworth Jan 25 '10 at 07:10
  • 2
    @Martin, perhaps you could post something to that effect in the question so that people don't waste their time trying to help. – overslacked Jan 25 '10 at 07:17
  • @overslacked, I will do that next time although to be clear I had the answer lined up and it was only a few minutes from when the question was posted until I posted the answer. Some people are just too quick for me :-) – Martin Hollingsworth Jan 26 '10 at 21:35
4

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.

Martin Hollingsworth
  • 7,249
  • 7
  • 49
  • 51