How to get the current product version in C#?

C#Reflection

C# Problem Overview


How can I programmatically get the current product version in C#?

My code:

VersionNumber = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

I am getting VersionNumber=1.0.0.0, but the current version is 1.0.0.12.

C# Solutions


Solution 1 - C#

There are three versions: assembly, file, and product. To get the product version:

using System.Reflection;
using System.Diagnostics;
Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fileVersionInfo.ProductVersion;

Solution 2 - C#

I got the answer to my question its Just give the reference to System.Deployment.Application and though it wont work in developement of the visual studio but it will work once the application is deployed.

//using System.Deployment.Application;
//using System.Reflection;
public string CurrentVersion
{
    get
    {
        return ApplicationDeployment.IsNetworkDeployed
               ? ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString()
               : Assembly.GetExecutingAssembly().GetName().Version.ToString();
    }
} 

 

Solution 3 - C#

System.Reflection.Assembly.GetEntryAssembly().GetName().Version

Solution 4 - C#

Another approach to getting the product version (which is specified using the AssemblyInformationalVersionAttribute) is

private static string AssemblyProductVersion
{
    get
    {
        object[] attributes = Assembly.GetExecutingAssembly()
            .GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false);
        return attributes.Length == 0 ?
            "" :
            ((AssemblyInformationalVersionAttribute)attributes[0]).InformationalVersion;
    }
}

Solution 5 - C#

Try this:

var thisApp = Assembly.GetExecutingAssembly();
AssemblyName name = new AssemblyName(thisApp.FullName);
VersionNumber = "v. " + name.Version;

Also, see this Microsoft Doc on the AssemblyName.Version property.

Solution 6 - C#

All these answers ask for the assembly with .GetExecutingAssembly().
If you have this code in a dll, it will return the dll version number.

Swap that call for GetCallingAssembly() to get the place in your code that wanted to know.

/// <summary>
/// Returns version like 2.1.15
/// </summary>
public static String ProductVersion
{
	get
	{
		return new Version(FileVersionInfo.GetVersionInfo(Assembly.GetCallingAssembly().Location).ProductVersion).ToString();
	}
}

Solution 7 - C#

In C# you need to use reflection and diagnostics

Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fileVersionInfo.ProductVersion;

Solution 8 - C#

I had the same issue as most of you. It would always show 1.0.0.0 unless you manually went in and updated assemblyInfo.cs to the version you wanted to display. I think we wanted to display the publish version-revision number under the project properties but that doesn't seem to be an option (from what I've read).

I'm not sure if back when these comments were made this existed, but now in the assemblyinfo.cs there is a way to do this automatically. I too was not content with having to manually update these with every publish.

// You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.0.*")]

That * auto-increments with each publish. It won't be the same as the publish number you see under the project properties, but it definitely increments and is definitely better than doing it by hand.

You then have a couple options to display it as mentioned above. I personally used this which I found on another site

Version version = Assembly.GetExecutingAssembly().GetName().Version; lblRevision.Text = String.Format("{0}.{1}.{2}.{3}", version.Major, version.Minor, version.Build, version.Revision);

Solution 9 - C#

var productVersion = FileVersionInfo.GetVersionInfo(typeof(SomeClassFromDesiredAssembly).Assembly.Location).ProductVersion;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNivid DholakiaView Question on Stackoverflow
Solution 1 - C#Tony HouView Answer on Stackoverflow
Solution 2 - C#Nivid DholakiaView Answer on Stackoverflow
Solution 3 - C#Ketan H KanadaView Answer on Stackoverflow
Solution 4 - C#Richard EvView Answer on Stackoverflow
Solution 5 - C#Glenn FerrieView Answer on Stackoverflow
Solution 6 - C#DefenestrationDayView Answer on Stackoverflow
Solution 7 - C#Frank WuView Answer on Stackoverflow
Solution 8 - C#blind SkwirlView Answer on Stackoverflow
Solution 9 - C#ChashitsuView Answer on Stackoverflow