How to get the publication version?

I would like to show the published version of my desktop application. I am trying to do this with this code:

_appVersion.Content = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; 

The problem is that I am not getting exactly the version of the publication that I have in my project properties. Below is a screenshot:

enter image description here

But I get 3.0.0.12546 . Does anyone know where the problem is?

+5
source share
4 answers

I also had this problem and I found that the version number set in AssemblyInfo.cs was interfering with one set in Properties :

 [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")] 

I usually comment on these lines from AssemblyInfo and replace them with

 [assembly: AssemblyVersion("1.0.*")] 

Check to see if these values ​​have been hardcoded into your AssemblyInfo file.

See this SO question for an interesting discussion of automatic version control. When checking AssemblyInfo.cs make sure that your auto-increment ( * - if you use it) targets only AssemblyVersion , not AssemblyFileVersion .


When debugging a program, you can check the assembly properties in

 \bin\Release\app.publish 

On the Details tab, check the version number. Is this consistent with any parameters specified in VS?

+5
source

We can create one property that will return version information as mentioned below, and we can use this property.

 public string VersionLabel { get { if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) { Version ver = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion; return string.Format("Product Name: {4}, Version: {0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision, Assembly.GetEntryAssembly().GetName().Name); } else { var ver = Assembly.GetExecutingAssembly().GetName().Version; return string.Format("Product Name: {4}, Version: {0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision, Assembly.GetEntryAssembly().GetName().Name); } } } 
+2
source
 System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; 

will provide you with the version of the assembly that exists in the AssemblyInfo.cs file, to get the publication version installed in the publication dialog, you should use

 System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion 

But note that you need to add a link to System.Deployment, and it will work only after publishing your application, by right-clicking on the project file and clicking on the publication, each time you publish it will increase the version.

If you tried to call the above line in debug mode, this will not work and will throw an exception, so you can use the following code:

 try { return System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion; } catch(Exception ex) { return Assembly.GetExecutingAssembly().GetName().Version; } 
0
source

Using C # 6.0 with Lambda Expression

 private string GetVersion => ApplicationDeployment.IsNetworkDeployed ? $"Version: {ApplicationDeployment.CurrentDeployment.CurrentVersion}" : $"Version: {Application.ProductVersion}"; 
0
source

All Articles