Keywords: .NET | Assembly Version | AssemblyFileVersion | AssemblyVersion | Version Management
Abstract: This article provides an in-depth exploration of assembly version management in the .NET framework, focusing on the distinctions and applications of AssemblyVersion and AssemblyFileVersion. Through the methods provided by the System.Diagnostics.FileVersionInfo class, it explains how to accurately obtain AssemblyFileVersion, complete with code examples and analysis of practical application scenarios. The article also offers professional guidance on selecting appropriate version management strategies for different project phases, combined with version control practices.
Fundamentals of Assembly Version Management
In .NET development, assembly version management is a critical aspect. The AssemblyInfo.cs file provides two core attributes to define different types of version numbers: AssemblyVersion and AssemblyFileVersion. Understanding the distinction between these two is essential for building robust applications.
Differences Between AssemblyVersion and AssemblyFileVersion
AssemblyVersion is the version number used by the framework during build and at runtime to locate, link, and load assemblies. When you add a reference to an assembly in your project, this version number is embedded. At runtime, the Common Language Runtime (CLR) uses this version number to load the assembly. It is important to note that this version is used along with the assembly's name, public key token, and culture information only if the assembly is strong-named (signed). If the assembly is not strong-named, only the file name is used for loading.
AssemblyFileVersion is the version number assigned to an assembly file in the file system. It is displayed by Windows Explorer and is never used by the .NET Framework or runtime for referencing. This version number is primarily used for file management and version tracking purposes.
Practical Method for Retrieving AssemblyFileVersion
To retrieve the AssemblyFileVersion, we can utilize the System.Diagnostics.FileVersionInfo class. Here is the specific implementation code:
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.FileVersion;
This code first obtains the currently executing assembly, then creates a FileVersionInfo instance via the assembly's location path, and finally extracts the file version information from that instance.
Complete Version Information Retrieval Example
In practical development, it is often necessary to obtain all relevant version information. The following is a comprehensive example:
// Get assembly version
string assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
// Get file version
string fileVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion;
// Get product version
string productVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion;
Recommended Version Control Strategies
Regarding version management strategies, it is advisable to tailor them to the specific needs of the project:
For framework assemblies or libraries that are updated frequently, maintaining a fixed AssemblyVersion while only updating the AssemblyFileVersion is a wise approach. This strategy allows developers to overwrite the assembly in the reference path without changing their project references, significantly simplifying the version management process.
For central or final release builds, it is recommended to update the AssemblyVersion to reflect significant changes. In such cases, the AssemblyFileVersion should typically be updated to match the AssemblyVersion to ensure consistency in version information.
Analysis of Practical Application Scenarios
In continuous integration and continuous deployment (CI/CD) environments, version management becomes particularly important. When using automated versioning tools like GitVersion, attention must be paid to the automatic generation strategies of version numbers. Some schemes may set different values for AssemblyVersion and AssemblyFileVersion, which requires developers to make adjustments based on specific needs.
For instance, in certain automated versioning schemes, AssemblyVersion might include commit count information, while AssemblyFileVersion might only include the major version number. Such differential settings necessitate that development teams clearly understand and establish corresponding version management standards.
Summary of Best Practices
In .NET assembly version management, it is recommended to adhere to the following best practices: clearly distinguish the purposes of AssemblyVersion and AssemblyFileVersion; maintain stability of AssemblyVersion in frequently updated scenarios; ensure consistency of version information in formal releases; and use appropriate tools and methods to obtain accurate version information.