【问题标题】:Version number in Winform form textWinform 表单文本中的版本号
【发布时间】:2011-11-02 23:18:25
【问题描述】:

如何将程序集版本号(我设置为自动递增)插入 Winform 表单文本?

【问题讨论】:

    标签: c# winforms visual-studio


    【解决方案1】:

    以下任何一种都可以:

    var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; 
    this.Text = String.Format("My Application Version {0}", version);
    
    string version = System.Windows.Forms.Application.ProductVersion; 
    this.Text = String.Format("My Application Version {0}", version);
    

    假设这是在您希望在其上显示文本的 Form 上运行的

    【讨论】:

    • @Mikhael - 如果它像 OP 一样在表单本身上运行,则默认情况下会添加此引用,但为了清楚起见,我会添加它以防在其他地方使用,谢谢跨度>
    【解决方案2】:
    Text = Application.ProductVersion
    

    以字符串形式获取完整版本的快速方法(例如“1.2.3.4”)

    【讨论】:

      【解决方案3】:

      我在 WinForm 中使用以下内容:

      public MainForm()
      {
        InitializeComponent();
        Version version = Assembly.GetExecutingAssembly().GetName().Version;
        Text = Text + " " + version.Major + "." + version.Minor + " (build " + version.Build + ")"; //change form title
      }
      

      不向用户显示修订号,内部版本号是足够的技术信息

      确保您的 AssemblyInfo.cs 以以下结尾(默认删除其中的版本),以便 VisualStudio 自动增加构建和修订号。您必须在每次发布时自己更新主要版本和次要版本(更新主要版本以获取新功能,次要版本仅在修复时):

      // Version information for an assembly consists of the following four values:
      //
      //      Major Version
      //      Minor Version 
      //      Build Number
      //      Revision
      //
      // You can specify all the values or you can default the Build and Revision Numbers 
      // by using the '*' as shown below:
      // [assembly: AssemblyVersion("1.0.*")]
      [assembly: AssemblyVersion("1.0.*")]
      

      【讨论】:

      • 不确定我的编辑是否已发布,无论如何也可以使用此修改:Version version = (ApplicationDeployment.IsNetworkDeployed)? ApplicationDeployment.CurrentDeployment.CurrentVersion:Assembly.GetExecutingAssembly().GetName().Version; //如果网络部署显示已发布版本(如网络安装页面所做的那样)
      【解决方案4】:

      它在 System.Reflection.AssemblyName 类中,例如。

      Assembly.GetExecutingAssembly().GetName().Version.ToString()
      

      【讨论】:

        【解决方案5】:

        如您所见:http://msdn.microsoft.com/en-us/library/system.reflection.assemblyname.version.aspx

        class Example
        {
            static void Main()
            {
                Console.WriteLine("The version of the currently executing assembly is: {0}",
                    Assembly.GetExecutingAssembly().GetName().Version);
        
                Console.WriteLine("The version of mscorlib.dll is: {0}",
                    typeof(String).Assembly.GetName().Version);
            }
        }
        

        【讨论】:

          【解决方案6】:
          System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
          System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location);
          return fvi.ProductVersion;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-06-24
            • 2013-05-07
            • 1970-01-01
            • 2012-02-08
            • 2016-03-12
            • 2014-02-11
            • 1970-01-01
            相关资源
            最近更新 更多