获取已发布的应用程序版本

时间:2014-03-14 15:11:51

标签: c# version-control .net-assembly

我的发布中的文件版本在我的应用程序执行时显示不同。我对此感到困惑,因为正如你在下面看到的那样,我的应用程序发布为1.0.0.2,很快就会发布为1.0.0.3,但显示为1.0.0.0。我的雇主已经要求装配版本,但我弄错了...如何返回正确的应用程序版本?谢谢!

我的发布信息屏幕: Publish Version

我的实际应用返回的图片:

enter image description here

最后但并非最不重要的是,代码:

public void setExecutingAssembly()
{
    FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assemblyVersion.Location);
    string appVersion = fvi.FileVersion;
    this.Text = string.Format(" {0} v{1}", this.Text, appVersion);
}

2 个答案:

答案 0 :(得分:1)

您可以使用ApplicationDeployment.CurrentDeployment.CurrentVersion获取版本字符串:

private static string clickOnceVersion()
{
    if (ApplicationDeployment.IsNetworkDeployed)
        return ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
    else
        return null;
}

此外,如果要从类库中的代码获取可执行文件的版本号,可以执行以下操作:

private static string programVersion()
{
    var assembly = Assembly.GetEntryAssembly(); // Version number of the calling exe, not this assembly!

    if (assembly != null)
        return Assembly.GetEntryAssembly().GetName().Version.ToString();
    else // Only happens if being called via reflection from, say, Visual Studio's Form Designer.
        return Assembly.GetExecutingAssembly().GetName().Version.ToString();
}

但是,我认为这是您需要的第一种方法。

答案 1 :(得分:0)

我相信你可以使用Reflection-

实现这一目标
Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
this.Text = (String.Format("{0}.{1}.{2}.{3}", version.Major, version.Minor,
            version.Revision, version.Build));