How to get the publish version of a WPF application How to get the publish version of a WPF application wpf wpf

How to get the publish version of a WPF application


Using Click Once, each time you publish, Visual Studio will change the number automatically. It will increment the value each time you publish. Your problem is that you have manually changed the number. The solution is to publish and just let Visual Studio update the value... you should notice that your project needs to be saved once you have published. This is because Visual Studio just incremented the value for you.


UPDATE >>>

If you want to access the published version from code (which you should have made clear in your question), then you can use this code, but you have to ensure that the application is network deployed first... that means that it has actually been published, so it won't work while you are debugging. Try this:

private string GetPublishedVersion(){    if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)    {        return System.Deployment.Application.ApplicationDeployment.CurrentDeployment.            CurrentVersion.ToString();    }    return "Not network deployed";}


You may be confused by 2 sets of numbers. Please note that you can set version of your WPF app in TWO different places:

  1. Project Properties / Publish tab / Publish Version
  2. AssemblyVersion declared in AssemblyInfo.cs file, which you can find if you expand Project Properties node in Solution Explorer.

They are similar in the sense that they both provides 4 numbers: major, minor, build and revision. The difference is that Publish Version is only available if the app was actually Published (i.e. Installed). It is not available in your debug session nor if you just copy the executable to another machine and run it there. SO, if you just need to track version of your EXE file, use AssemblyInfo.cs.

Correspondingly, to read the data use the following code:


1 To read Publish version (declared in Publish tab)

using System.Deployment.Application;ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();

Note that in this case: a) you need to add reference to System.Deployment assembly, b) if the app was not deployed, it won't work.


2 To read Assembly Version (declared in AssemblyInfo.cs)

Assembly.GetExecutingAssembly().GetName().Version;

This one always works.


Universal solution if we get application version from not startup assembly:

var version = System.Reflection.Assembly.GetEntryAssembly().GetName().Version;string appVersion = $"{version.Major}.{version.Minor}";

GetEntryAssembly give version of startup project.