33

I want to read and display WPF application publish version number in splash windows, In project properties in publish tab there is publish version, how can I get this and display it in WPF windows.

Thanks in advance

Abdulsalam Elsharif
  • 4,773
  • 7
  • 32
  • 66

4 Answers4

62

Access the assembly version using Assembly.GetExecutingAssembly() and display in UI

Assembly.GetExecutingAssembly().GetName().Version.ToString();
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
31

Add reference to System.Deployment library to your project and adjust this snippet to your code:

using System.Deployment.Application;

and

string version = null;
try
{   
    //// get deployment version
    version = ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
}
catch (InvalidDeploymentException)
{
    //// you cannot read publish version when app isn't installed 
    //// (e.g. during debug)
    version = "not installed";
}

As stated in comment, you cannot obtain publish version during debug, so I suggest to handle InvalidDeploymentException.

Bru
  • 514
  • 3
  • 14
  • 3
    If you start by checking ApplicationDeployment.IsNetworkDeployed this will let you know if CurrentDeployment is available. This way you don't have to handle the exception, but can make a simple if-else. – aliceraunsbaek Aug 13 '16 at 09:24
  • This answers half of the question, but not the part about displaying it in the WPF display. How do you bind a component to then use this version variable? – njminchin Aug 08 '18 at 06:32
  • Is this still valid in .net core? – hybrid2102 May 04 '20 at 07:34
  • @hybrid2102 It works well. I used these codes on WPF .NET Core 3.1. I added text, Version 1.1.1.1, to Project -> Properties -> Package Tab -> Package Version. I can get the right string on runtime (includes Debug). – cmcromance Sep 16 '20 at 01:41
8

Use GetEntryAssembly rather than GetExecutingAssembly to get the version from the current executable rather than from the currently-executing DLL, like so:

string version = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString();
Clonkex
  • 3,373
  • 7
  • 38
  • 55
innomatrix.ch
  • 89
  • 1
  • 1
5
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); 

Console.WriteLine(version);
Masoud Siahkali
  • 5,100
  • 1
  • 29
  • 18
  • I don't understand why that this reply and the reply below described the same answer, but one get +17 but another get -1 – Smith.Lai Nov 16 '17 at 03:01
  • 7
    @Smith.Lai because is the same answer, except that 2 years after. – Erre Efe Apr 10 '18 at 14:07
  • @Smith.Lai You're right, I've edited the [other answer](https://stackoverflow.com/a/44134308/2288578) to clean it up a bit. It's more valid than this answer, which is just a dupe of the [top-voted answer](https://stackoverflow.com/a/23459838/2288578). – Clonkex Feb 03 '22 at 23:22