当前位置: 首页 > wzjs >正文

HTML做网站的书籍黄冈市住房和城乡建设厅网站

HTML做网站的书籍,黄冈市住房和城乡建设厅网站,wordpress 标签html代码,网站备案空间备案前言 欢迎关注dotnet研习社,今天我们讨论一个Winform开发中的一个常见的需求内容“关于程序的版本号显示”。 在 WinForms 桌面应用程序开发中,向用户显示当前程序的版本号是一个常见的需求,尤其是在产品发布、更新提示或技术支持场景中尤为…

在这里插入图片描述

前言

欢迎关注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;public partial class MainForm : Form
{public MainForm(){InitializeComponent();var version = Assembly.GetExecutingAssembly().GetName().Version;Label lblVersion = new Label{Text = $"程序集版本:{version}",AutoSize = true,Location = new Point(20, 20)};this.Controls.Add(lblVersion);}
}

说明:

在这里插入图片描述

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

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

示例代码:

在窗体中添加了 StatusStripToolStripStatusLabel 控件,命名为 statusStrip1toolStripStatusLabel1

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

添加步骤:

在窗体中添加了 menuStriptoolStripMenuItem 控件,命名为 menuStrip1toolStripMenuItem1

  1. 添加 → 新建项 → “关于框(About Box)”
  2. AboutBox1.cs 修改版本号设置:
partial class AboutBox1 : 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 程序集特性访问器public string 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);}}public string AssemblyVersion{get{return Assembly.GetExecutingAssembly().GetName().Version.ToString();}}public string AssemblyDescription{get{object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);if (attributes.Length == 0){return "";}return ((AssemblyDescriptionAttribute)attributes[0]).Description;}}public string AssemblyProduct{get{object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);if (attributes.Length == 0){return "";}return ((AssemblyProductAttribute)attributes[0]).Product;}}public string AssemblyCopyright{get{object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);if (attributes.Length == 0){return "";}return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;}}public string AssemblyCompany{get{object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);if (attributes.Length == 0){return "";}return ((AssemblyCompanyAttribute)attributes[0]).Company;}}#endregion
}
  1. 调用方式:
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{new AboutBox1().ShowDialog();
}

在这里插入图片描述

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

准备版本文件:

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

1.2.3+build.12345

示例代码:

public partial class MainForm : 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(20, 50)};this.Controls.Add(lbl);}
}

在这里插入图片描述

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

using System.Reflection;
using System.Diagnostics;public static class VersionHelper
{public static string AssemblyVersion =>Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "Unknown";public static string FileVersion =>FileVersionInfo.GetVersionInfo(Application.ExecutablePath).FileVersion ?? "Unknown";public static string ProductVersion =>Application.ProductVersion ?? "Unknown";
}

调用方式:

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

在这里插入图片描述

对比总结

方式编号获取方式来源(csproj 或程序集)示例输出推荐用途特点说明
Application.ProductVersion<InformationalVersion>(或 <Version>1.2.3-betaUI显示(标题栏、关于框、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 显示可组合 ①~⑤自由定制标准“关于”窗口常用于商业软件,集中展示版本、版权等

🎯 推荐选择指南

  • 开发初期快速显示:使用 Application.ProductVersion
  • 需要对比程序集版本绑定:使用 AssemblyVersion
  • 需要展示文件详细版本(如系统托盘右键):使用 FileVersionInfo
  • 需要区分构建版本(多环境发布):结合 CI 写入 version.txt
  • 面向最终用户展示:统一写入 AboutBox,使用封装工具类读取版本
http://www.dtcms.com/wzjs/572235.html

相关文章:

  • 解答网站内容优化策略文友胜做的网站
  • 朔州网站seo深圳网络优化
  • 国外网站设计网站东莞市网络seo推广服务机构
  • 网站项目开发的一般流程辽宁住房建设厅网站首页
  • 去别人网站挂黑链做代理能赚到钱吗
  • 网站建设公司沈阳代理注册
  • 网站规划建设与管理维护教程与实训电脑软件开发是什么专业
  • 搜索引擎和门户网站的区别wordpress地址支持中文
  • 用动易建设网站教程wordpress plugin zip
  • 企业网站开发软件建设田达摩托车官方网站
  • 安徽建设银行 招聘网站做网站的重要性
  • 好的建网站的书籍网站建设及编辑岗位职责
  • 网站建设推荐华网天下外贸网站建设流程
  • wordpress适用于图片站的主题做区域县城招聘网站
  • 东莞市建设企业网站服务机构网站投注员怎么做
  • 百度做网站吗一般通过是什么意思
  • 电子商务网站建设实践报告网站底部友情链接怎么做的
  • wordpress主题视频站wordpress客户端linux
  • 网站加搜索框广州个人网站制作公司
  • 官方网站在哪里米拓企业网站管理系统
  • 网站qq登录 开发显示危险网站怎么解决
  • dw软件做的网站怎么发到网上新思维网站
  • 买布做衣裳 在哪个网站买好桂林到阳朔怎么走最方便
  • 网站访问量过大儿童 摄影 wordpress 模板
  • 建设网站需要的资源网站编辑 seo
  • 中国城镇建设网站高校里做网站的工作
  • 做购物网站要多少钱网站即时到账要怎么做
  • 网站设计苏州建筑设计公司有哪些部门
  • 企腾做的网站怎么样软件开发公司照片
  • 随州网站seo多少钱前端做网站框架