How do I get the current published version in a .NET application?
Asked Answered
P

6

39

I want to be able to display the current version of a .NET application that I have deployed using the publish wizard. There is a nice option to automatically update the version number every time I publish my application.

I found another question (Automatically update version number) that had this to get the current version:

Assembly.GetExecutingAssembly().GetName().Version

This gets you the version you set in the project properties, but not the version that is automatically incremented each time you publish.

Partlow answered 8/8, 2009 at 12:58 Comment(2)
Can you qualify that assertion somehow? GetExecutingAssembly().GetName().Version works just fine on release assembliesShackelford
Maybe I meant publish and not deploy. I can go change that in the question. When I run through the publish wizard it automatically updates a publish version. In code it is referred to as the Deployed version.Partlow
P
45

I ended up using this little bit of code to get the current deployed version or if it isn't deployed the current assembly version.

private Version GetRunningVersion()
{
  try
  {
    return Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;
  }
  catch
  {
    return Assembly.GetExecutingAssembly().GetName().Version;
  }
}

I had to add references to System.Deployment and System.Reflection.

Partlow answered 8/8, 2009 at 13:2 Comment(3)
You don't have to use that hackish try/catch block. You can check to see if your app is deployed ApplicationDeployment.IsNetworkDeployedJohnnyjumpup
ApplicationDeployment.IsNetworkDeployed throws an exception if it is not running as a ClickOnce application and the result may be closing the entire application. You do want the try-catch - otherwise you are doing a disservice to current and/or future users.Metabolize
After adding ApplicationDeployment.IsNetworkDeployed, I never saw an exception again. It was only when accessing the CurrentDeployment class.Hine
C
53

You can use the following test

if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) {
    return System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;
}

to avoid the exception (as detailed in this post).

Also, I don't think you can get the current publish version via Visual Studio debugging because accessing CurrentDeployment will throw an InvalidDeploymentException.

Continuance answered 12/4, 2011 at 19:20 Comment(2)
I prefer this method over the accepted answer. The accepted answer makes no distinction about which exception it's handling. It's generally bad practice to make sweeping exception handlers.Shaner
Likewise. It also doesn't revert arbitrarily to the assembly version; it's not comparable to the deployment version, so it shouldn't be used as a fallback.Zacharia
P
45

I ended up using this little bit of code to get the current deployed version or if it isn't deployed the current assembly version.

private Version GetRunningVersion()
{
  try
  {
    return Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;
  }
  catch
  {
    return Assembly.GetExecutingAssembly().GetName().Version;
  }
}

I had to add references to System.Deployment and System.Reflection.

Partlow answered 8/8, 2009 at 13:2 Comment(3)
You don't have to use that hackish try/catch block. You can check to see if your app is deployed ApplicationDeployment.IsNetworkDeployedJohnnyjumpup
ApplicationDeployment.IsNetworkDeployed throws an exception if it is not running as a ClickOnce application and the result may be closing the entire application. You do want the try-catch - otherwise you are doing a disservice to current and/or future users.Metabolize
After adding ApplicationDeployment.IsNetworkDeployed, I never saw an exception again. It was only when accessing the CurrentDeployment class.Hine
O
4

I used following solution for this problem, and it is working for me:

DataSet ds = new DataSet();
ds.ReadXml(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MyProd.application"));
DataTable dt = new DataTable();
if (ds.Tables.Count > 1) {
    dt = ds.Tables[1];
    MessageBox.Show(dt.Rows[0]["version"].ToString());
}
Oecology answered 12/12, 2016 at 20:38 Comment(2)
Upvote because it is simple and a light operation, whereas, even MS doesn't provide a good way to access this.Hine
This is the actual correct solution. I just used it. ThanksDesmid
O
2

Based in the answer from Jason, I ended up with this:

Add Reference to System.Deployment.

string versionDeploy = Application.ProductVersion;              
if (System.Diagnostics.Debugger.IsAttached)
{
    this.lblVersion.Caption = string.Format("Versión {0} DESA", versionDeploy);
}
else
{
    if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
    {
        Version Deploy = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;
        versionDeploy = string.Format("{0}.{1}.{2}.{3}", Deploy.Major, Deploy.Minor, Deploy.Build, Deploy.Revision);
    }
    this.lblVersion.Caption = string.Format("Versión {0} PROD", versionDeploy);
}

Hope it helps.

Olmos answered 6/6, 2016 at 14:1 Comment(0)
S
0
using System.Deployment.Application;

and

string yourPublishedVersionNumber=ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString()
Slime answered 17/5, 2018 at 12:50 Comment(1)
Worth checking for ApplicationDeployment.IsNetworkDeployed for when you run it locally, as otherwise the ApplicationDeployment.CurrentDeployment throws an exceptionMeadowsweet
A
-1
Imports System.Configuration
Public Function GetAppVersion() As String
    Dim ass As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
    Dim ver As System.Version = ass.GetName().Version
    Return ver.Major & "." & ver.Minor & "." & ver.Revision
End Function
Australia answered 31/7, 2013 at 2:48 Comment(1)
Not only is it the question, but it is important to note that this grabs the application version NOT the publication version. I have it set to auto-increment on an Excel AddIn, so every publish comes out 1.0.0.138 or something similar while the above code returns 1.0.0.0 which is set under the properties tab.Hine

© 2022 - 2024 — McMap. All rights reserved.