Reputation: 147
The assemblyinfo.cs file has the AssemblyVersion attribute, but when I run the following:
Attribute[] y = Assembly.GetExecutingAssembly().GetCustomAttributes();
I get:
System.Runtime.InteropServices.ComVisibleAttribute
System.Runtime.CompilerServices.RuntimeCompatibilityAttribute
System.Runtime.CompilerServices.CompilationRelaxationsAttribute
System.Runtime.InteropServices.GuidAttribute
System.Diagnostics.DebuggableAttribute
System.Reflection.AssemblyTrademarkAttribute
System.Reflection.AssemblyCopyrightAttribute
System.Reflection.AssemblyCompanyAttribute
System.Reflection.AssemblyConfigurationAttribute
System.Reflection.AssemblyFileVersionAttribute
System.Reflection.AssemblyProductAttribute
System.Reflection.AssemblyDescriptionAttribute
and yet I have checked countless times that this attribute is present in my code:
[assembly: AssemblyVersion("5.5.5.5")]
...and if I try to access it directly I get an exception:
Attribute x = Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyVersionAttribute)); //exception
I guess I won't be able to use that attribute, but how come .NET isn't reading it?
Upvotes: 7
Views: 2710
Reputation: 147
I will repeat Hans Passant's comment:
[AssemblyVersion] is a really big deal in .NET. The compiler treats the attribute specially, it uses it when it generates the metadata of the assembly. And doesn't actually emit the attribute, that would be doing it twice. Use the AssemblyName.Version instead, as shown.
Upvotes: 4
Reputation: 16934
(just to round out the flavors of getting a version...)
If you're trying to get file version info on an arbitrary assembly (i.e., not one loaded/running), you can use FileVersionInfo
- however, note that this may not be the same AssemblyVersion
specified in the metadata:
var filePath = @"c:\path-to-assembly-file";
FileVersionInfo info = FileVersionInfo.GetVersionInfo(filePath);
// the following two statements are roughly equivalent
Console.WriteLine(info.FileVersion);
Console.WriteLine(string.Format("{0}.{1}.{2}.{3}",
info.FileMajorPart,
info.FileMinorPart,
info.FileBuildPart,
info.FilePrivatePart));
Upvotes: 0
Reputation: 10163
If you're trying to just get the assembly version, it's pretty straight forward:
Console.WriteLine("The version of the currently executing assembly is: {0}", Assembly.GetExecutingAssembly().GetName().Version);
The property is a type of System.Version, which has Major
, Minor
, Build
, and Revision
properties.
Eg. an assembly of version 1.2.3.4
has:
Major
= 1
Minor
= 2
Build
= 3
Revision
= 4
Upvotes: 9