LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

在 .NET 8 开发的WinForms 程序中展示程序版本号的几种方式

admin
2025年5月13日 19:12 本文热度 8

前言

欢迎关注dotnet研习社,今天我们讨论一个Winform开发中的一个常见的需求内容“关于程序的版本号显示”。

在 WinForms 桌面应用程序开发中,向用户显示当前程序的版本号是一个常见的需求,尤其是在产品发布、更新提示或技术支持场景中尤为重要。在.NET 8 中已全面采用 SDK 风格项目,相比旧的 .NET Framework 项目,版本号的设置

和读取方式更加规范和现代化。本文将介绍在 WinForms 应用中显示程序版本号的几种常见方式,并附上示

例代码,供大家参

☑ 项目准备

确保我们的 .csproj 是 SDK 风格,并配置版本号:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

<PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>

    <!-- 版本信息设置 -->
    <Version>1.2.3</Version>
    <FileVersion>1.2.3.0</FileVersion>
    <AssemblyVersion>1.2.0.0</AssemblyVersion>
    <InformationalVersion>1.2.3-beta</InformationalVersion>
</PropertyGroup>

</Project>

✅ 示例 1:窗体标题栏显示版本号(使用 Application.ProductVersion

示例代码:

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
        this.Text = $"我的程序 - 版本 {Application.ProductVersion}";
    }
}

说明:

  • • 输出示例:我的程序 - 版本 1.2.3-beta
  • • 适用于:简洁快速展示,适合主界面。

✅ 示例 2:Label 中显示版本号(使用 AssemblyVersion

示例代码:

using System.Reflection;

publicpartialclassMainForm : Form
{
    public MainForm()
    {
        InitializeComponent();

        var version = Assembly.GetExecutingAssembly().GetName().Version;
        Label lblVersion = new Label
        {
            Text = $"程序集版本:{version}",
            AutoSize = true,
            Location = new Point(2020)
        };

        this.Controls.Add(lblVersion);
    }
}

说明:

  • • 输出示例:程序集版本:1.2.0.0
  • • 适用于:开发或内部测试查看版本绑定。

✅ 示例 3:状态栏中显示版本号(使用 FileVersionInfo

示例代码:

在窗体中添加了 StatusStrip 和 ToolStripStatusLabel 控件,命名为 statusStrip1 和 toolStripStatusLabel1

using System.Diagnostics;

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();

        var info = FileVersionInfo.GetVersionInfo(Application.ExecutablePath);
        toolStripStatusLabel1.Text = $"文件版本:{info.FileVersion}";
    }
}

说明:

  • • 输出示例:文件版本:1.2.3.0
  • • 适用于:状态栏、底部信息区。

✅ 示例 4:AboutBox 显示版本号(使用 Application.ProductVersion

添加步骤:

在窗体中添加了 menuStrip 和 toolStripMenuItem 控件,命名为 menuStrip1 和 toolStripMenuItem1

  1. 1. 添加 → 新建项 → “关于框(About Box)”
  2. 2. 在 AboutBox1.cs 修改版本号设置:
partial classAboutBox1 : Form
{
    public AboutBox1()
    {
        InitializeComponent();
        this.Text = String.Format("关于 {0}", AssemblyTitle);
        this.labelProductName.Text = AssemblyProduct;
        this.labelVersion.Text = String.Format("版本 {0}", AssemblyVersion);
        this.labelCopyright.Text = AssemblyCopyright;
        this.labelCompanyName.Text = AssemblyCompany;
        this.textBoxDescription.Text = AssemblyDescription;
    }

    #region 程序集特性访问器

    publicstring AssemblyTitle
    {
        get
        {
            object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
            if (attributes.Length > 0)
            {
                AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                if (titleAttribute.Title != "")
                {
                    return titleAttribute.Title;
                }
            }
            return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
        }
    }

    publicstring AssemblyVersion
    {
        get
        {
            return Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }
    }

    publicstring AssemblyDescription
    {
        get
        {
            object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
            if (attributes.Length == 0)
            {
                return"";
            }
            return ((AssemblyDescriptionAttribute)attributes[0]).Description;
        }
    }

    publicstring AssemblyProduct
    {
        get
        {
            object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
            if (attributes.Length == 0)
            {
                return"";
            }
            return ((AssemblyProductAttribute)attributes[0]).Product;
        }
    }

    publicstring AssemblyCopyright
    {
        get
        {
            object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
            if (attributes.Length == 0)
            {
                return"";
            }
            return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
        }
    }

    publicstring AssemblyCompany
    {
        get
        {
            object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
            if (attributes.Length == 0)
            {
                return"";
            }
            return ((AssemblyCompanyAttribute)attributes[0]).Company;
        }
    }
    #endregion
}
  1. 3. 调用方式:
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
    new AboutBox1().ShowDialog();
}

✅ 示例 5:读取外部版本文件(CI 自动生成 version.txt

准备版本文件:

项目发布后输出目录含有 version.txt 内容如:

1.2.3+build.12345

示例代码:

public partialclassMainForm : Form
{
    public MainForm()
    {
        InitializeComponent();

        string versionFile = Path.Combine(AppContext.BaseDirectory, "version.txt");
        string buildVersion = File.Exists(versionFile) ? File.ReadAllText(versionFile).Trim() : "Unknown";

        Label lbl = new Label
        {
            Text = $"构建版本:{buildVersion}",
            AutoSize = true,
            Location = new Point(2050)
        };
        this.Controls.Add(lbl);
    }
}

✅ 示例 6:统一封装 VersionHelper 工具类

using System.Reflection;
using System.Diagnostics;

publicstaticclassVersionHelper
{
    publicstaticstring AssemblyVersion =>
        Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "Unknown";

    publicstaticstring FileVersion =>
        FileVersionInfo.GetVersionInfo(Application.ExecutablePath).FileVersion ?? "Unknown";

    publicstaticstring ProductVersion =>
        Application.ProductVersion ?? "Unknown";
}

调用方式:

Label lbl = new Label
{
    Text = $"程序集版本:{VersionHelper.AssemblyVersion}\n文件版本:{VersionHelper.FileVersion}",
    AutoSize = true,
    Location = new Point(2080)
};
this.Controls.Add(lbl);


对比总结

方式编号
获取方式
来源(csproj 或程序集)
示例输出
推荐用途
特点说明
Application.ProductVersion<InformationalVersion>
(或 <Version>
1.2.3-beta
UI显示(标题栏、关于框、Label)
默认最直观,获取产品版本,强烈推荐
Assembly.GetExecutingAssembly().GetName().Version<AssemblyVersion>1.2.0.0
内部模块依赖、调试
获取程序集绑定版本,不一定展示给用户
FileVersionInfo.FileVersion<FileVersion>1.2.3.0
状态栏、日志、故障排查
Windows 文件属性中可见的“文件版本”
FileVersionInfo.ProductVersion<InformationalVersion>
(或 <Version>
1.2.3-beta
技术支持、版本详情
和 Application.ProductVersion 一致
读取 version.txt、嵌入资源等
CI/CD 或 Git 自动生成
1.2.3+g123abc
内部构建版本控制
灵活但需配合构建脚本或 CI 工具
自定义 AboutBox 显示
可组合 ①~⑤
自由定制
标准“关于”窗口
常用于商业软件,集中展示版本、版权等


阅读原文:原文链接





该文章在 2025/5/14 10:11:49 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved