226

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?

7 Answers 7

376

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.

Sign up to request clarification or add additional context in comments.

8 Comments

If it's deployed with ClickOnce, you need to check System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion
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
@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).
@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 :)
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))
|
46

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.

Comments

24
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.

1 Comment

+1. GetEntryAssembly (vs GetCallingAssembly or GetExecutingAssembly) seems to be the only thing that works when called from within a reference library.
11

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

Comments

7

This should do:

Assembly assem = Assembly.GetExecutingAssembly();
AssemblyName aName = assem.GetName();
return aName.Version.ToString();

1 Comment

this 3 lines could become 1 : return Assembly.GetExecutingAssembly().GetName().Version.ToString(); .............
4

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

Comments

1

This seemed to work with Git Versioning

Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version

1 Comment

+1. I also found <AssemblyVersionAttribute>()?.Version (.NET assembly loader uses this as part of the identity of the assembly, not shown in Windows file properties, can be null) and <AssemblyInformationalVersionAttribute>()?.InformationalVersion (the "Product Version", I don't know if this is always defined or can be null also).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.