How can I get the assembly file version
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When developing software, especially that which is version controlled or distributed, it's important to track different versions of assemblies created as part of .NET applications. Assembly versioning helps in dependency management, compatibility checks, and maintenance in production environments. Here, we discuss how you can retrieve the assembly file version effectively.
Understanding Assembly Versions
In .NET, assemblies contain executable code in the form of Intermediate Language (IL) along with metadata that includes version information. There are typically two types of version numbers associated with a .NET assembly:
- Assembly Version: This is the version used by the runtime for binding and can affect the application's compatibility with different versions of the same assembly.
- File Version: Primarily used for informational purposes, this version helps in identifying the particular build of the assembly. It does not affect the assembly binding.
How to Specify Assembly Versions
Before we get into how to retrieve these versions, it's important to know how they are set. In a .NET project, the assembly version and file version can be specified in the AssemblyInfo.cs file or via attributes in the project itself. Here’s an example:
Retrieving the Assembly File Version
To get the file version of an assembly, you utilize classes provided in the System.Reflection namespace. Below is a detailed method on how you can achieve this:
Using Assembly Attributes
- Reference the Required Namespace: Make sure to include
System.Reflectionin your using directives.
- Load the Assembly: If the assembly is already part of your process (i.e., you are retrieving the version of the assembly your code is in), you can get the currently executing assembly:
To check the version of another assembly, load it by specifying its file path or name:
- Get the File Version Information: Use the
GetCustomAttributemethod to extract theAssemblyFileVersionAttributefrom the loaded assembly.
Practical Example
Here’s a complete snippet that can be used in a .NET Console application:
This example will print the file version of the executable running the code.
Conclusion and Key Points
Retrieving the file version of an assembly in .NET is straightforward using the System.Reflection namespace. Here’s a brief summary of key points:
| Attribute | Namespace | Method Used | Used For |
AssemblyFileVersion | System.Reflection | GetCustomAttribute | Identifying build of the assembly (informational) |
AssemblyVersion | System.Reflection | GetCustomAttribute | Used by runtime for binding |
While the file version is largely informational and crucial from a software maintenance or audit perspective, the assembly version is critical for application functionality, particularly how different versions of the same assembly interact within the .NET runtime. This understanding is essential for robust .NET application development and maintenance.

