213

I am trying to get the executing assembly version in C# 3.0 using the following code:

var assemblyFullName = Assembly.GetExecutingAssembly().FullName;
var version = assemblyFullName .Split(',')[1].Split('=')[1];

Is there another proper way of doing so?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1
  • 2,179
  • 2
  • 12
  • 7

6 Answers6

356

Two options... regardless of application type you can always invoke:

Assembly.GetExecutingAssembly().GetName().Version

If a Windows Forms application, you can always access via application if looking specifically for product version.

Application.ProductVersion

Using GetExecutingAssembly for an assembly reference is not always an option. As such, I personally find it useful to create a static helper class in projects where I may need to reference the underlying assembly or assembly version:

// A sample assembly reference class that would exist in the `Core` project.
public static class CoreAssembly
{
    public static readonly Assembly Reference = typeof(CoreAssembly).Assembly;
    public static readonly Version Version = Reference.GetName().Version;
}

Then I can cleanly reference CoreAssembly.Version in my code as required.

Chris Baxter
  • 16,083
  • 9
  • 51
  • 72
  • 7
    If it's deployed with ClickOnce, you need to check `System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion` – Justin Morgan Feb 09 '11 at 04:36
  • What would be interesting: Where do you need to specify the actual version? I specified the AssemblyVersion and AssemblyFileVersion in my AssemblyInfo.cs and all I get from the abovemethod call is: 0.0.0.0 despite having specified 1.0.0 – AgentKnopf Jun 07 '13 at 09:35
  • @Zainodis If you are referring to the `ClickOnce` version mentioned by @Justin, it is specified on the `Publish` tab within the project properties (i.e., not related to AssemblyVersion or AssemblyFileVersion). – Chris Baxter Jun 07 '13 at 12:55
  • @CalgaryCoder thanks for the response, I don't know what ClickOnce is, but it turned out when editing the AssemblyInfo file via VS then it suddenly worked oO. Editing the AssemblyInfo.cas manually in a text editor made all fields turn up empty. Either way, it's solved now :) – AgentKnopf Jun 07 '13 at 13:19
  • 8
    It's worth mentioning that this doesn't work in ASP.NET MVC apps, because of the launch context. A workaround is to reference a known Type in your website assembly, e.g. the ubiquitous MVC `HomeController`, so in Razor: `v@(Assembly.GetAssembly(typeof(MyWebProject.Mvc.Controllers.HomeController)).GetName().Version.ToString(2))` – James McCormack Mar 06 '14 at 15:19
  • @JamesMcCormack I might shorten to `typeof(HomeController).Assembly`, regardless you are correct that executing assembly isn't always the best choice. – Chris Baxter Mar 06 '14 at 15:25
  • `Application.ProductVersion` returns AssemblyFileVersion. `Assembly.GetExecutingAssembly().GetName().Version` returns AssemblyVersion. – Vladimir Kocjancic Mar 04 '16 at 23:28
  • for the record: in PCL you will have to use `Assembly Reference = typeof(CoreAssembly).GetTypeInfo().Assembly;` – Gradient Sep 05 '17 at 11:47
45

In MSDN, Assembly.GetExecutingAssembly Method, is remark about method "getexecutingassembly", that for performance reasons, you should call this method only when you do not know at design time what assembly is currently executing.

The recommended way to retrieve an Assembly object that represents the current assembly is to use the Type.Assembly property of a type found in the assembly.

The following example illustrates:

using System;
using System.Reflection;

public class Example
{
    public static void Main()
    {
        Console.WriteLine("The version of the currently executing assembly is: {0}",
                          typeof(Example).Assembly.GetName().Version);
    }
}

/* This example produces output similar to the following:
   The version of the currently executing assembly is: 1.1.0.0

Of course this is very similar to the answer with helper class "public static class CoreAssembly", but, if you know at least one type of executing assembly, it isn't mandatory to create a helper class, and it saves your time.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sharunas Bielskis
  • 1,033
  • 1
  • 16
  • 25
23
using System.Reflection;
{
    string version = Assembly.GetEntryAssembly().GetName().Version.ToString();
}

Remarks from MSDN http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getentryassembly%28v=vs.110%29.aspx:

The GetEntryAssembly method can return null when a managed assembly has been loaded from an unmanaged application. For example, if an unmanaged application creates an instance of a COM component written in C#, a call to the GetEntryAssembly method from the C# component returns null, because the entry point for the process was unmanaged code rather than a managed assembly.

DDA
  • 991
  • 10
  • 28
Hammad Qureshi
  • 231
  • 2
  • 2
  • 2
    +1. `GetEntryAssembly` (vs `GetCallingAssembly` or `GetExecutingAssembly`) seems to be the only thing that works when called from within a reference library. – drzaus May 29 '14 at 19:31
10

Product Version may be preferred if you're using versioning via GitVersion or other versioning software.

To get this from within your class library you can call System.Diagnostics.FileVersionInfo.ProductVersion:

using System.Diagnostics;
using System.Reflection;

//...

var assemblyLocation = Assembly.GetExecutingAssembly().Location;
var productVersion = FileVersionInfo.GetVersionInfo(assemblyLocation).ProductVersion

enter image description here

Daniel
  • 8,655
  • 5
  • 60
  • 87
7

This should do:

Assembly assem = Assembly.GetExecutingAssembly();
AssemblyName aName = assem.GetName();
return aName.Version.ToString();
bluish
  • 26,356
  • 27
  • 122
  • 180
Elroy Flynn
  • 3,032
  • 1
  • 24
  • 33
  • 6
    this 3 lines could become 1 : return Assembly.GetExecutingAssembly().GetName().Version.ToString(); ............. – Alexandre May 26 '16 at 15:19
3

I finally settled on typeof(MyClass).GetTypeInfo().Assembly.GetName().Version for a netstandard1.6 app. All of the other proposed answers presented a partial solution. This is the only thing that got me exactly what I needed.

Sourced from a combination of places:

https://msdn.microsoft.com/en-us/library/x4cw969y(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/2exyydhb(v=vs.110).aspx

Victor Ude
  • 415
  • 4
  • 13