【问题标题】:how to get publish version?如何获得发布版本?
【发布时间】:2017-02-04 22:07:12
【问题描述】:

我想展示我的桌面应用程序的发布版本。我正在尝试使用此代码:

_appVersion.Content = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

问题是我没有得到我在项目属性中的确切发布版本。下面是它的截图:

但我收到了3.0.0.12546。有人知道问题出在哪里吗?

【问题讨论】:

  • 您是否在调试模式下检查?
  • 不,处于发布模式

标签: c# visual-studio desktop-application


【解决方案1】:

我们可以创建一个属性来返回版本信息 如下所述,我们可以使用该属性。

public string VersionLabel
{
    get
    {
        if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
        {
            Version ver = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;
            return string.Format("Product Name: {4}, Version: {0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision, Assembly.GetEntryAssembly().GetName().Name);
        }
        else
        {
            var ver = Assembly.GetExecutingAssembly().GetName().Version;
            return string.Format("Product Name: {4}, Version: {0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision, Assembly.GetEntryAssembly().GetName().Name);
        }
    }
}

【讨论】:

  • 好的,所以我把它放在 Main.CS 中,当我把它调用到一个字符串时,我收到了 1.0.7379.32346,但我的版本是 20.03.8.3。为什么我得到错误的版本号?谢谢戴夫
  • 我刚刚将软件安装到我的笔记本电脑(部署),我得到了正确的版本号以显示在主标题文本中。感谢您提供出色的解决方案。
【解决方案2】:

我也遇到了这个问题,发现AssemblyInfo.cs中设置的版本号干扰了Properties中设置的版本号:

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

我通常将这些行从AssemblyInfo 中注释掉并替换为

[assembly: AssemblyVersion("1.0.*")]

检查这些值是否已硬编码到您的AssemblyInfo 文件中。

请参阅this SO question,了解有关自动版本控制的有趣讨论。检查AssemblyInfo.cs 时,请确保您的自动增量(* - 如果您正在使用它)仅针对AssemblyVersion 而不是AssemblyFileVersion


调试程序时,可以查看程序集的属性

\bin\Release\app.publish

Details 标签下,检查版本号。这是否与您在 VS 中指定的任何设置相匹配?

【讨论】:

  • 完美,您的解决方案非常有效,非常感谢!
【解决方案3】:
System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

将为您获取 AssemblyInfo.cs 文件中存在的程序集版本,要获取您在发布对话框中设置的发布版本,您应该使用

System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion

但请注意,您必须添加对 System.Deployment 的引用,并且它只有在您通过右键单击项目文件并单击发布来发布您的应用程序后才会起作用,每次发布时,它都会增加 Revision。

如果您尝试在调试模式下调用上述行,它将不起作用并会引发异常,因此您可以使用以下代码:

try
{
    return System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;
}
catch(Exception ex)
{
    return Assembly.GetExecutingAssembly().GetName().Version;
}

【讨论】:

  • 请注意,第二种方式有效,并在应用发布时显示发布版本,但直接从 Visual Studio 启动应用时会抛出错误。
【解决方案4】:

将 C# 6.0 与 Lambda 表达式结合使用

private string GetVersion => ApplicationDeployment.IsNetworkDeployed ? $"Version: {ApplicationDeployment.CurrentDeployment.CurrentVersion}" : $"Version: {Application.ProductVersion}";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-30
    • 2016-09-06
    • 1970-01-01
    • 2018-03-02
    • 2012-04-01
    • 2012-11-11
    • 1970-01-01
    • 2012-05-22
    相关资源
    最近更新 更多