ChatGPT解决这个技术问题 Extra ChatGPT

How can I get the executing assembly version?

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?


C
Chris Baxter

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.


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))
P
Peter Mortensen

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.


D
DDA
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. GetEntryAssembly (vs GetCallingAssembly or GetExecutingAssembly) seems to be the only thing that works when called from within a reference library.
D
Daniel

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

https://i.stack.imgur.com/Cio5P.png


b
bluish

This should do:

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

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

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