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
{
static void Main()
{
string path = Assembly.GetEntryAssembly().Location;
FileVersionInfo info = FileVersionInfo.GetVersionInfo(path);
Console.WriteLine(info.FileVersion);
}
}
How it works
Assembly.GetEntryAssembly().Locationgives the file path of the executable.FileVersionInfo.GetVersionInfo(path)reads the Win32 file metadata.info.FileVersionreturns the file version.
Which one should you use?
- Use
AssemblyFileVersionAttributewhen you want the attribute value directly from the assembly metadata. - Use
FileVersionInfowhen you want the actual file version resource from the compiled file.
For many applications, either works well.
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:
[assembly: AssemblyFileVersion("1.0.2.0")]
then the output will be:
1.0.2.0
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
A project may try multiple version sources:
- file version
- informational version
- assembly version
This is useful when running in test environments, plugins, or unusual hosts.
4. UI and diagnostics separation
Teams often use:
AssemblyInformationalVersionfor display text like1.2.0-betaAssemblyFileVersionfor support/debuggingAssemblyVersionfor binary compatibility decisions
That separation keeps versioning cleaner across build and release systems.
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)
{
string path = assembly.Location;
}
Mistake 3: Using the wrong assembly
If you are inside a class library, GetEntryAssembly() gets the main app, not necessarily the current library.
Fix
Use typeof(SomeTypeInYourLibrary).Assembly when you need the library's version.
Assembly assembly = typeof(MyClass).Assembly;
Mistake 4: Expecting a Version object from AssemblyFileVersion
AssemblyVersion returns a Version object, but AssemblyFileVersion is commonly handled as a string.
string fileVersion = attribute?.Version;
Mistake 5: Confusing AssemblyFileVersion with AssemblyInformationalVersion
AssemblyInformationalVersion may include text like 1.0.0-beta, while AssemblyFileVersion is usually numeric.
Use the right one for your goal.
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 | AssemblyInformationalVersionAttribute | 1.0.2-beta+45 | Display in UI, release labels |
GetEntryAssembly() vs GetExecutingAssembly() vs typeof(...).Assembly
| API | Returns | Best used when |
|---|---|---|
Assembly.GetEntryAssembly() | The process entry assembly | You want the main app version |
Assembly.GetExecutingAssembly() | The currently executing assembly | You want the assembly containing the running code |
typeof(MyType).Assembly | The assembly containing MyType | You want a specific library's version |
For many desktop apps, GetEntryAssembly() is the right choice. For libraries, typeof(MyType).Assembly is often safer.
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
using System.Reflection;
using System.Diagnostics;
Safer helper
string GetFileVersion()
{
Assembly assembly = Assembly.GetEntryAssembly();
if (assembly == null) return "Unknown";
var attribute = assembly.GetCustomAttribute<AssemblyFileVersionAttribute>();
return attribute?.Version ?? "Unknown";
}
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?
Yes. That is very common in real projects.
What if GetEntryAssembly() returns null?
Use a fallback such as Assembly.GetExecutingAssembly() or typeof(MyType).Assembly, depending on which assembly you actually want.
Can I use this for DLLs as well as EXEs?
Yes. You just need to get the correct assembly or file path for the DLL you want to inspect.
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# Access Modifiers and static: public, private, protected, and Defaults
Learn how C# public, private, protected, and default access control visibility, and how static differs from instance members.