How to get the current version of a product in C #?

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

My code is:

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

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

+49
c # windows
Jun 27 '11 at 13:37
source share
8 answers

I got the answer to my question: just give a link to System.Deployment.Application, and although it will not work in Visual Studio development, it will work after 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(); } } 
+27
Jun 27 '11 at 21:45
source share

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; 
+66
Sep 12 '11 at 1:00 a.m.
source share
 System.Reflection.Assembly.GetEntryAssembly().GetName().Version 
+15
Jun 18 '16 at 5:13
source share

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

 private static string AssemblyProductVersion { get { object[] attributes = Assembly.GetExecutingAssembly() .GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false); return attributes.Length == 0 ? "" : ((AssemblyInformationalVersionAttribute)attributes[0]).InformationalVersion; } } 
+6
Nov 15 '11 at 11:00
source share

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

Change this call to GetCallingAssembly() to get the place in the code that you need.

 /// <summary> /// Returns version like 2.1.15 /// </summary> public static String ProductVersion { get { return new Version(FileVersionInfo.GetVersionInfo(Assembly.GetCallingAssembly().Location).ProductVersion).ToString(); } } 
+3
29 Oct '15 at 16:30
source share

Try the following:

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

Also see this MSDN article on the AssemblyName.Version property.

+2
Jun 27 2018-11-11T00:
source share

In C # you need to use reflection and diagnostics

 Assembly assembly = Assembly.GetExecutingAssembly(); FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location); string version = fileVersionInfo.ProductVersion; 
+2
Jul 31 '14 at 7:43
source share

I had the same problem as most of you. It will always show 1.0.0.0 if you did not enter manually and did not update the assemblyInfo.cs file to the version you wanted to display. I think that we wanted to show the version number of the edition in the project properties, but this does not seem to be an option (from what I read).

I'm not sure that once, when these comments were made, it existed, but now in the assemblyinfo.cs file there is a way to do this automatically. I also did not like to update them manually with each publication.

//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.*")]

This * automatically increases with every post. It will not be the same as the publication number that you see in the project properties, but it is definitely increasing, and certainly better than doing it manually.

Then you have several options to display as 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);

0
Jul 31 '18 at 22:13
source share



All Articles