Question
In a C# project, AssemblyInfo can define both AssemblyVersion and AssemblyFileVersion:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.2.0")]
AssemblyVersion represents the version identity of the assembly, while AssemblyFileVersion is the Win32 file version resource and does not need to match the assembly version.
I can read AssemblyVersion like this:
Version version = Assembly.GetEntryAssembly().GetName().Version;
How can I get AssemblyFileVersion in C#?
Short Answer
By the end of this page, you will understand the difference between AssemblyVersion and AssemblyFileVersion, how to read file version information in C#, when to use reflection versus file metadata APIs, and how this is commonly handled in real applications.
Concept
AssemblyVersion and AssemblyFileVersion are related, but they serve different purposes.
AssemblyVersionis part of the assembly's identity.AssemblyFileVersionis file metadata stored in the compiled executable or DLL.
In C#, you usually read them in different ways:
AssemblyVersionis commonly accessed throughAssembly.GetName().VersionAssemblyFileVersionis commonly accessed through:- the
AssemblyFileVersionAttribute, or FileVersionInfo
- the
Why this matters:
- You may want to show a version string in an About dialog.
- You may log the exact deployed build number.
- Support teams may need the file version to identify a release.
- CI/CD systems often increment file version independently from assembly identity.
A key point for beginners: AssemblyFileVersion is not the same thing as AssemblyVersion. Even if both are defined in the same project file, they can contain different values and are used for different reasons.
Mental Model
Think of an assembly like a book:
AssemblyVersionis the edition number used by libraries and the runtime to identify the book.AssemblyFileVersionis the printing number stamped on the physical copy.
Two books might be the same edition, but printed at different times with different print numbers. In the same way, an assembly can keep the same AssemblyVersion while the AssemblyFileVersion changes for each build.
Syntax and Examples
The two most common ways to get AssemblyFileVersion are shown below.
Option 1: Read the AssemblyFileVersionAttribute
using System;
using System.Reflection;
class Program
{
static void Main()
{
Assembly assembly = Assembly.GetEntryAssembly();
var attribute = assembly.GetCustomAttribute<AssemblyFileVersionAttribute>();
string fileVersion = attribute?.Version;
Console.WriteLine(fileVersion);
}
}
How it works
GetEntryAssembly()gets the main executable assembly.GetCustomAttribute<AssemblyFileVersionAttribute>()reads the attribute applied to that assembly.attribute.Versioncontains the file version string.
Option 2: Use FileVersionInfo
using System;
using System.Diagnostics;
using System.Reflection;
class Program
{
()
{
path = Assembly.GetEntryAssembly().Location;
FileVersionInfo info = FileVersionInfo.GetVersionInfo(path);
Console.WriteLine(info.FileVersion);
}
}
Step by Step Execution
Consider this example:
using System;
using System.Reflection;
class Program
{
static void Main()
{
Assembly assembly = Assembly.GetEntryAssembly();
var attribute = assembly.GetCustomAttribute<AssemblyFileVersionAttribute>();
Console.WriteLine(attribute?.Version ?? "No file version found");
}
}
Step by step:
Assembly.GetEntryAssembly()gets the assembly that started the process.- That result is stored in the
assemblyvariable. GetCustomAttribute<AssemblyFileVersionAttribute>()searches the assembly for theAssemblyFileVersionattribute.- If the attribute exists, it returns an object representing that attribute.
attribute?.Versionsafely reads theVersionproperty.- If
attributeisnull, the null-coalescing operator??prints"No file version found"instead.
If your assembly contains this:
Real World Use Cases
AssemblyFileVersion is commonly used in practical situations such as:
- About screens: showing the exact installed build in a desktop app.
- Logging: writing the deployed application version to logs during startup.
- Diagnostics: helping support teams identify which binary a customer is running.
- Update tools: comparing installed file versions with available releases.
- Installer validation: confirming the correct executable or DLL was deployed.
Example: startup logging
using System;
using System.Diagnostics;
using System.Reflection;
class Program
{
static void Main()
{
string path = Assembly.GetEntryAssembly().Location;
string version = FileVersionInfo.GetVersionInfo(path).FileVersion;
Console.WriteLine($"Starting app version {version}");
}
}
Real Codebase Usage
In real projects, developers usually wrap version access in a helper method or service instead of repeating reflection code everywhere.
Common patterns
1. Centralized version helper
using System.Diagnostics;
using System.Reflection;
public static class AppVersion
{
public static string GetFileVersion()
{
string path = Assembly.GetEntryAssembly()?.Location;
if (string.IsNullOrEmpty(path))
return "Unknown";
return FileVersionInfo.GetVersionInfo(path).FileVersion ?? "Unknown";
}
}
This makes version lookup easy to reuse.
2. Guard clauses
Real code often checks for null because GetEntryAssembly() can return null in some hosting scenarios.
Assembly assembly = Assembly.GetEntryAssembly();
if (assembly == null)
return;
3. Fallback strategies
Common Mistakes
Here are some common mistakes beginners make.
Mistake 1: Assuming AssemblyVersion and AssemblyFileVersion are the same
Broken assumption:
Version version = Assembly.GetEntryAssembly().GetName().Version;
Console.WriteLine(version);
This reads AssemblyVersion, not AssemblyFileVersion.
Fix
Use AssemblyFileVersionAttribute or FileVersionInfo.
Mistake 2: Forgetting that GetEntryAssembly() can be null
Broken code:
string path = Assembly.GetEntryAssembly().Location;
If GetEntryAssembly() is null, this throws an exception.
Fix
Assembly assembly = Assembly.GetEntryAssembly();
if (assembly != null)
{
path = assembly.Location;
}
Comparisons
Here is a quick comparison of the main version-related values in .NET.
| Concept | What it represents | Common API | Example value | Typical use |
|---|---|---|---|---|
AssemblyVersion | Assembly identity | Assembly.GetName().Version | 1.0.0.0 | Runtime binding, compatibility |
AssemblyFileVersion | Win32 file version resource | AssemblyFileVersionAttribute or FileVersionInfo | 1.0.2.0 | Diagnostics, file properties, support |
AssemblyInformationalVersion | Human-readable product version |
Cheat Sheet
Quick reference
Get AssemblyVersion
Version version = Assembly.GetEntryAssembly().GetName().Version;
Get AssemblyFileVersion from attribute
string fileVersion = Assembly
.GetEntryAssembly()?
.GetCustomAttribute<AssemblyFileVersionAttribute>()?
.Version;
Get AssemblyFileVersion from file metadata
string path = Assembly.GetEntryAssembly()?.Location;
string fileVersion = FileVersionInfo.GetVersionInfo(path).FileVersion;
Rules to remember
AssemblyVersionis assembly identity.AssemblyFileVersionis file metadata.- They can be different.
GetEntryAssembly()may returnnull.- For class libraries, use
typeof(MyType).Assemblyif you want that library's version.
Useful namespaces
FAQ
How do I get AssemblyFileVersion in C#?
Use either AssemblyFileVersionAttribute through reflection or FileVersionInfo with the assembly file path.
What is the difference between AssemblyVersion and AssemblyFileVersion?
AssemblyVersion is used for assembly identity and binding. AssemblyFileVersion is file metadata used mainly for diagnostics and file properties.
Why does Assembly.GetName().Version not return the file version?
Because it returns the assembly identity version, which comes from AssemblyVersion, not the file version resource.
Should I use FileVersionInfo or AssemblyFileVersionAttribute?
Use AssemblyFileVersionAttribute for direct attribute access. Use FileVersionInfo when you specifically want the file's version resource.
Can AssemblyFileVersion be different from AssemblyVersion?
Mini Project
Description
Build a small C# console app that prints different version values for the current application. This helps you see the difference between assembly identity, file version, and informational version in a practical way.
Goal
Create a program that reads and displays AssemblyVersion, AssemblyFileVersion, and AssemblyInformationalVersion for the current application.
Requirements
- Read the current application's assembly.
- Print the
AssemblyVersion. - Print the
AssemblyFileVersion. - Print the
AssemblyInformationalVersion. - Show
Unknownif a value cannot be found.
Keep learning
Related questions
AddTransient vs AddScoped vs AddSingleton in ASP.NET Core Dependency Injection
Learn the differences between AddTransient, AddScoped, and AddSingleton in ASP.NET Core DI with examples and practical usage.
Best Way to Repeat a Character in C#: Building Repeated Strings Efficiently
Learn the best way to repeat a character in C#, compare StringBuilder, string concatenation, and simpler built-in options.
C# Array Initialization Syntaxes Explained
Learn all common C# array initialization syntaxes with examples, rules, comparisons, and mistakes beginners often make.