【问题标题】:how to show publish version in a textbox?如何在文本框中显示发布版本?
【发布时间】:2019-01-25 15:48:54
【问题描述】:

目前,我每次发布应用程序时都会手动更新应用程序中的版本字段 (textbox)。我想知道是否有办法让我的应用程序从某个地方获取该数据并为我显示在框中。我正在使用 VS2012,但我不确定如何在 C# 中实现这一点。下面是我正在谈论的 VS2012 属性窗口的屏幕截图。

新图片:

【问题讨论】:

    标签: c#


    【解决方案1】:

    不要忘记检查应用程序是否已部署网络,否则它将无法在调试模式下运行。

    if (ApplicationDeployment.IsNetworkDeployed)
    {
        this.Text = string.Format("Your application name - v{0}",
            ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString(4));
    }
    

    【讨论】:

    • IsNetworkDeployed 返回 false.. 可能是什么问题?
    • 当你在IDE下运行debug版本时,根据定义你不会被网络部署,ApplicationDeployment.CurrentDeployment.CurrentVersion会抛出System.Deployment.Application.InvalidDeploymentException,所以你必须检查IsNetworkDeployed。要测试它是否有效,请将其发布到网络然后部署它!或者,您可以根据@Christos Lytras 的回答尝试Application.ProductVersion
    【解决方案2】:

    试试这个:

    using System.Deployment.Application;
    
    public Version AssemblyVersion 
    {
        get
        {
            return ApplicationDeployment.CurrentDeployment.CurrentVersion;
        }
    }
    

    然后 getter 属性的调用者可以取消引用 MajorMinorBuildRevision 属性,如下所示:

    YourVersionTextBox.Text = AssemblyVersion.Major.ToString() + "."
                            + AssemblyVersion.Minor.ToString() + "."
                            + AssemblyVersion.Build.ToString() + "."
                            + AssemblyVersion.Revision.ToString();
    

    【讨论】:

    • 我尝试了您的建议,但是在 try 块中,即使在包含“使用”部分之后,applicationdeployment 也会出现错误“system.windows.forms.application 不包含 applicationdeployment 的定义”跨度>
    • 您的 Windows 窗体项目中是否引用了 System.Deployment.dll
    • 我确实这样做了,我觉得令人费解的是它一开始就有问题。
    • 我在问题中添加了一张图片,它不会让我编译应用程序,
    • D'oh,刚刚意识到 Application 是命名空间,而 using 需要是 System.Deployment.Application;,更新答案。此外,您的属性需要返回Version 而不是string,因为Version 包含您要在文本框中复制的MajorMinorBuildRevision 属性。
    【解决方案3】:

    我们也可以使用重载ToString of System.Version

    using System.Deployment.Application;
    
    public Version AssemblyVersion 
    {
        get
        {
            return ApplicationDeployment.CurrentDeployment.CurrentVersion;
        }
    }
    
    
    YourVersionTextBox.Text = AssemblyVersion.ToString(1); // 1 - get only major
    YourVersionTextBox.Text = AssemblyVersion.ToString(2); // 1.0 - get only major, minor
    YourVersionTextBox.Text = AssemblyVersion.ToString(3); // 1.0.3 - get only major, minor, build
    YourVersionTextBox.Text = AssemblyVersion.ToString(4); // 1.0.3.4 - get only major, minor, build, revision
    

    【讨论】:

    • AssemblyVersion != 部署版本
    【解决方案4】:

    方法一: 你可以用这个

    string version = Application.ProductVersion;
    

    并在您的文本框中显示版本

    方法二: 或者如果你想要单独的版本部分,你可以使用这个:

    System.Version version2 = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
    

    现在你有了这些:

    version2.Major;
    version2.Minor;
    version2.Revision;
    version2.Build;
    

    你可以像这样使用它们

    string versionString= (String.Format("{0}.{1}.{2}.{3}", version2.Major, version2.Minor, version2.Revision, version2.Build));
    

    【讨论】:

    • 这是汇编版本,不是发布版本
    【解决方案5】:

    如果你得到ApplicationDeployment的错误,那么你可以试试这个:

    对于通过Program访问的全局版本,在Program类中声明:

    private static Version version = new Version(Application.ProductVersion);
    
    public static Version Version
    {
        get
        {
            return version;
        }
    }
    

    现在您在程序中的任何位置都有Program.Version,您可以使用它来获取版本信息,如下所示:

    LabelVersion.Text = String.Format("Version {0}.{1}",
        Program.Version.Major.ToString(),
        Program.Version.Minor.ToString());
    

    【讨论】:

      猜你喜欢
      • 2020-10-31
      • 2023-01-10
      • 1970-01-01
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多