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

时间:2009-08-08 12:58:56

标签: c# .net

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

我发现了另一个问题( Automatically update version number ),以获取当前版本:

Assembly.GetExecutingAssembly().GetName().Version

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

6 个答案:

答案 0 :(得分:50)

您可以使用以下测试

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

以避免异常(详见this post)。

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

答案 1 :(得分:45)

我最终使用这一小段代码来获取当前部署的版本,或者它是否未部署当前的程序集版本。

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

我必须添加对System.DeploymentSystem.Reflection的引用。

答案 2 :(得分:2)

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

添加对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);
}

希望它有所帮助。

答案 3 :(得分:2)

我使用以下解决方案来解决这个问题,它对我有用:

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 :(得分:0)

using System.Deployment.Application;

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

答案 5 :(得分:-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
相关问题