views:

1991

answers:

3

Hi Folk, The assembly version I can get with following loc:

Version version = Assembly.GetEntryAssembly().GetName().Version;

But how can I get the assembly file version?

Thanks for help.

A: 
public static string GetProductVersion()
{
  var attribute = (AssemblyVersionAttribute)Assembly
    .GetExecutingAssembly()
    .GetCustomAttributes( typeof(AssemblyVersionAttribute), true )
    .Single();
   return attribute.InformationalVersion;
}

(From http://bytes.com/groups/net/420417-assemblyversionattribute - as noted there, if you're lookign for a different attribute, substitute that into the above)

Ruben Bartelink
Hey Ruben, 2 notes. First, the question asked for AssemblyFileVersion not AssemblyVersion. Second, Assembly.GetExecutingAssembly().GetCustomAttributes( typeof(AssemblyVersionAttribute), true ) returns an array of length 0. I think this is because AssemblyVersionAttribute is not a custom attribute.
Iain
Re the first point, thats why I said "if you're lookign for a different attribute, substitute that into the above" (IIRC I didnt try it out). Re the second, that does seem plausible but dont have time to dig in...
Ruben Bartelink
+19  A: 

See my comment above asking for clarification on what you really want. Hopefully this is it:

Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.ProductVersion;
Xiaofu
Jap, this is exactly what I search for, thanks.
Enyra
+1  A: 

There are three versions: assembly, file, and product. They are used by different features and take on different default values if you don't explicit specify them. For more info see: http://all-things-pure.blogspot.com/2009/09/assembly-version-file-version-product.html.

Check6