Question

How to Get AssemblyFileVersion in C#

csharp.netassembliesversion

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.

  • AssemblyVersion is part of the assembly's identity.
  • AssemblyFileVersion is file metadata stored in the compiled executable or DLL.

In C#, you usually read them in different ways:

  • AssemblyVersion is commonly accessed through Assembly.GetName().Version
  • AssemblyFileVersion is commonly accessed through:
    • the AssemblyFileVersionAttribute, or
    • FileVersionInfo

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:

  • AssemblyVersion is the edition number used by libraries and the runtime to identify the book.
  • AssemblyFileVersion is 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.

Take Quiz

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.Version contains 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().Location gives the file path of the executable.
  • FileVersionInfo.GetVersionInfo(path) reads the Win32 file metadata.
  • info.FileVersion returns the file version.

Which one should you use?

  • Use AssemblyFileVersionAttribute when you want the attribute value directly from the assembly metadata.
  • Use FileVersionInfo when 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:

  1. Assembly.GetEntryAssembly() gets the assembly that started the process.
  2. That result is stored in the assembly variable.
  3. GetCustomAttribute<AssemblyFileVersionAttribute>() searches the assembly for the AssemblyFileVersion attribute.
  4. If the attribute exists, it returns an object representing that attribute.
  5. attribute?.Version safely reads the Version property.
  6. If attribute is null, 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}");
    }
}
Take Quiz

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:

  • AssemblyInformationalVersion for display text like 1.2.0-beta
  • AssemblyFileVersion for support/debugging
  • AssemblyVersion for 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.

ConceptWhat it representsCommon APIExample valueTypical use
AssemblyVersionAssembly identityAssembly.GetName().Version1.0.0.0Runtime binding, compatibility
AssemblyFileVersionWin32 file version resourceAssemblyFileVersionAttribute or FileVersionInfo1.0.2.0Diagnostics, file properties, support
AssemblyInformationalVersionHuman-readable product versionAssemblyInformationalVersionAttribute1.0.2-beta+45Display in UI, release labels

GetEntryAssembly() vs GetExecutingAssembly() vs typeof(...).Assembly

APIReturnsBest used when
Assembly.GetEntryAssembly()The process entry assemblyYou want the main app version
Assembly.GetExecutingAssembly()The currently executing assemblyYou want the assembly containing the running code
typeof(MyType).AssemblyThe assembly containing MyTypeYou want a specific library's version

For many desktop apps, GetEntryAssembly() is the right choice. For libraries, typeof(MyType).Assembly is often safer.

Take Quiz

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

  • AssemblyVersion is assembly identity.
  • AssemblyFileVersion is file metadata.
  • They can be different.
  • GetEntryAssembly() may return null.
  • For class libraries, use typeof(MyType).Assembly if 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.

Take Quiz

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

  1. Read the current application's assembly.
  2. Print the AssemblyVersion.
  3. Print the AssemblyFileVersion.
  4. Print the AssemblyInformationalVersion.
  5. Show Unknown if a value cannot be found.
Take Quiz

Keep learning

Related questions

View more