Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the publish version of a WPF application

Tags:

c#

wpf

clickonce

I want my WPF application publish version. I tried using the answer for this question. It works but the problem is we can manually change the values there. I want to know how many times my project was actually published (don't need version number. Just how many times did I publish my application). Can this be done?

like image 297
Raj123 Avatar asked Mar 20 '14 08:03

Raj123


3 Answers

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";
}
like image 190
Sheridan Avatar answered Oct 18 '22 20:10

Sheridan


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.

like image 10
Mike Keskinov Avatar answered Oct 18 '22 19:10

Mike Keskinov


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.

like image 4
Danil Shaykhutdinov Avatar answered Oct 18 '22 21:10

Danil Shaykhutdinov