We talked about the three assembly version attributes, AssemblyVersion, AssemblyFileVersion and AssemblyInformationalVersion, previously. Now let us see how to retrieve these attributes programmatically from an assembly.
It seems natural that the GetCustomAttribute as demonstrated before will do the work again.
Here is some sample code:
Assembly asm = Assembly.GetExecutingAssembly();
string output = string.Empty;
Attribute att = AssemblyFileVersionAttribute.GetCustomAttribute(asm, typeof(AssemblyFileVersionAttribute));
AssemblyFileVersionAttribute fileverAtt = att as AssemblyFileVersionAttribute;
output += "AssemblyFileVersion: ";
if (fileverAtt == null)
output += "Not found!";
else
output += fileverAtt.Version;
output += Environment.NewLine;
att = AssemblyInformationalVersionAttribute.GetCustomAttribute(asm, typeof(AssemblyInformationalVersionAttribute));
AssemblyInformationalVersionAttribute infoverAtt = att as AssemblyInformationalVersionAttribute;
output += "AssemblyInformationalVersion: ";
if (infoverAtt == null)
output += "Not found!";
else
output += infoverAtt.InformationalVersion;
output += Environment.NewLine;
att = AssemblyVersionAttribute.GetCustomAttribute(asm, typeof(AssemblyVersionAttribute));
AssemblyVersionAttribute verAtt = att as AssemblyVersionAttribute;
output += "AssemblyVersion: ";
if (verAtt == null)
output += "Not found!";
else
output += verAtt.Version;
output += Environment.NewLine;
output += "Version from AssemblyName: " + asm.GetName().Version.ToString();
MessageBox.Show(output);
Here is the output:
The AssemblyFileVersion and the AssemblyInformationalVersion can be successfully retrieved by the GetCustomAttribute() call of the relevant attribute type as expected and verified, but the AssemblyVersion cannot! It might suggest that the AssemblyVersion is not among custom assembly attributes at all!!
The workaround is to use the AssemblyName and call its Version property as demonstrated.
AssemblyInfo Updater of RevitAddinWidget can help review popular assembly information of all available projects in a Visual Studio solution and update the information conveniently and flexibly in a single central place.
Recent Comments