Ed Haber
Ed Haber

Reputation: 1877

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

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.

Upvotes: 40

Views: 50727

Answers (6)

George
George

Reputation: 693

using System.Deployment.Application;

and

string yourPublishedVersionNumber=ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString()

Upvotes: 0

Manpreet Singh Dhillon
Manpreet Singh Dhillon

Reputation: 903

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());
}

Upvotes: 4

BlueMystic
BlueMystic

Reputation: 2287

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.

Upvotes: 2

Mark Micallef
Mark Micallef

Reputation: 2788

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

Upvotes: 0

Jason
Jason

Reputation: 8640

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.

Upvotes: 53

Ed Haber
Ed Haber

Reputation: 1877

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.

Upvotes: 45

Related Questions