How can I read WPF publish version number in code behind
Asked Answered
V

4

35

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

Vernacularize answered 4/5, 2014 at 18:12 Comment(0)
S
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.

Silvern answered 4/5, 2014 at 22:11 Comment(4)
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.Cyclo
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?Muntjac
Is this still valid in .net core?Ashworth
@Ashworth 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).Francophobe
W
66

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

Assembly.GetExecutingAssembly().GetName().Version.ToString();
Woodhead answered 4/5, 2014 at 18:15 Comment(7)
Thanks for your time, It works but always set the version 1.0.0.0 although my application version is 1.0.0.158 !Vernacularize
@AbdusalamElsherif where do you have the version? inside the assemblyinfo?Woodhead
Adding the Assembly Information in this way definitely works.Listerism
@AbdulsalamElsharif, i think this is because you are using this code snippet from another project(another assembly). For example your main Windows-app version is 1.0.2, but your dll-project is 1.0.0.Impost
AssemblyVersion not working if you deploying using "Publish" method.Impost
Isn't this the file version and not the package version?Selfmoving
Using Automatic Versioning can make dealing with version numbers a lot easier.Welter
S
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.

Silvern answered 4/5, 2014 at 22:11 Comment(4)
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.Cyclo
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?Muntjac
Is this still valid in .net core?Ashworth
@Ashworth 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).Francophobe
B
9

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();
Belak answered 23/5, 2017 at 12:2 Comment(0)
U
5
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); 

Console.WriteLine(version);
Uvarovite answered 17/9, 2016 at 9:32 Comment(3)
I don't understand why that this reply and the reply below described the same answer, but one get +17 but another get -1Cortege
@Cortege because is the same answer, except that 2 years after.Deform
@Cortege You're right, I've edited the other answer to clean it up a bit. It's more valid than this answer, which is just a dupe of the top-voted answer.Hunch

© 2022 - 2024 — McMap. All rights reserved.