如何在.NET应用程序中获取当前发布的版本?

时间:2022-04-30 20:07:19

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.

我希望能够显示我使用发布向导部署的.NET应用程序的当前版本。每次发布应用程序时都有一个很好的选项可以自动更新版本号。

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.

这将获取您在项目属性中设置的版本,但不会获取每次发布时自动递增的版本。

6 个解决方案

#1


48  

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.

此外,我认为您无法通过Visual Studio调试获取当前发布版本,因为访问CurrentDeployment将抛出InvalidDeploymentException。

#2


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.

我不得不添加对System.Deployment和System.Reflection的引用。

#3


2  

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

#4


1  

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

根据杰森的回答,我最终得到了这个:

Add Reference to System.Deployment.

添加对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.

希望能帮助到你。

#5


0  

using System.Deployment.Application;

and

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

#6


-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

#1


48  

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.

此外,我认为您无法通过Visual Studio调试获取当前发布版本,因为访问CurrentDeployment将抛出InvalidDeploymentException。

#2


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.

我不得不添加对System.Deployment和System.Reflection的引用。

#3


2  

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

#4


1  

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

根据杰森的回答,我最终得到了这个:

Add Reference to System.Deployment.

添加对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.

希望能帮助到你。

#5


0  

using System.Deployment.Application;

and

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

#6


-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