Assembly File
File Version
Software Development
Coding Guide
Programming Help

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:

  1. 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.
  2. 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:

csharp
1using System.Reflection;
2
3[assembly: AssemblyVersion("1.0.0.0")]
4[assembly: AssemblyFileVersion("1.0.123.456")]

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

  1. Reference the Required Namespace: Make sure to include System.Reflection in your using directives.
csharp
   using System.Reflection;
  1. 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:
csharp
   Assembly assembly = Assembly.GetExecutingAssembly();

To check the version of another assembly, load it by specifying its file path or name:

csharp
   Assembly assembly = Assembly.Load("AssemblyName");
  1. Get the File Version Information: Use the GetCustomAttribute method to extract the AssemblyFileVersionAttribute from the loaded assembly.
csharp
   AssemblyFileVersionAttribute fileVersionInfo = assembly.GetCustomAttribute(typeof(AssemblyFileVersionAttribute)) as AssemblyFileVersionAttribute;
   string fileVersion = fileVersionInfo.Version;

Practical Example

Here’s a complete snippet that can be used in a .NET Console application:

csharp
1using System;
2using System.Reflection;
3
4class Program {
5    static void Main() {
6        Assembly assembly = Assembly.GetExecutingAssembly();
7        AssemblyFileVersionAttribute fileVersionInfo = assembly.GetCustomAttribute(typeof(AssemblyFileVersionAttribute)) as AssemblyFileVersionAttribute;
8        
9        Console.WriteLine("File Version: " + fileVersionInfo.Version);
10    }
11}

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:

AttributeNamespaceMethod UsedUsed For
AssemblyFileVersionSystem.ReflectionGetCustomAttributeIdentifying build of the assembly (informational)
AssemblyVersionSystem.ReflectionGetCustomAttributeUsed 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.


Course illustration
Course illustration

All Rights Reserved.