37

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.

Community
  • 1
  • 1
Ed Haber
  • 1,797
  • 2
  • 14
  • 14
  • 1
    Can you qualify that assertion somehow? GetExecutingAssembly().GetName().Version works just fine on release assemblies – Sam Saffron Aug 08 '09 at 13:02
  • 1
    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. – Ed Haber Aug 08 '09 at 13:05

6 Answers6

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.

Daniel
  • 10,864
  • 22
  • 84
  • 115
Jason
  • 8,400
  • 10
  • 56
  • 69
  • 3
    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. – Daniel Jan 18 '13 at 20:24
  • 1
    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. – Asherah Apr 12 '13 at 05:33
44

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.

Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
Ed Haber
  • 1,797
  • 2
  • 14
  • 14
  • 6
    You don't have to use that hackish try/catch block. You can check to see if your app is deployed `ApplicationDeployment.IsNetworkDeployed` – The Muffin Man Aug 11 '14 at 02:31
  • 1
    `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. – Peter Mortensen Mar 12 '15 at 20:40
  • After adding ApplicationDeployment.IsNetworkDeployed, I never saw an exception again. It was only when accessing the CurrentDeployment class. – Anthony Mason Feb 17 '17 at 14:40
3

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());
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
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.

Jhollman
  • 2,093
  • 24
  • 19
0
using System.Deployment.Application;

and

string yourPublishedVersionNumber=ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString()
George
  • 653
  • 6
  • 18
  • Worth checking for `ApplicationDeployment.IsNetworkDeployed` for when you run it locally, as otherwise the `ApplicationDeployment.CurrentDeployment` throws an exception – Michal Ciechan Oct 21 '19 at 10:38
-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
Mark Micallef
  • 2,643
  • 7
  • 28
  • 36
  • 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. – Anthony Mason Feb 17 '17 at 13:07