.Net?Winform開發(fā)顯示程序版本號的常見方式
歡迎關注dotnet研習社,今天我們討論一個Winform開發(fā)中的一個常見的需求內(nèi)容“關于程序的版本號顯示”。
在 WinForms 桌面應用程序開發(fā)中,向用戶顯示當前程序的版本號是一個常見的需求,尤其是在產(chǎn)品發(fā)布、更新提示或技術支持場景中尤為重要。在.NET 8 中已全面采用 SDK 風格項目,相比舊的 .NET Framework 項目,版本號的設置和讀取方式更加規(guī)范和現(xiàn)代化。本文將介紹在 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
- 適用于:開發(fā)或內(nèi)部測試查看版本綁定。
示例 3:狀態(tài)欄中顯示版本號
使用 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
- 適用于:狀態(tài)欄、底部信息區(qū)。
示例 4:AboutBox 顯示版本號
使用 Application.ProductVersion
添加步驟:
在窗體中添加了 menuStrip
和 toolStripMenuItem
控件,命名為 menuStrip1
和 toolStripMenuItem1
。
- 添加 → 新建項 → “關于框(About Box)”
- 在
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 }
調(diào)用方式:
private void toolStripMenuItem1_Click(object sender, EventArgs e) { new AboutBox1().ShowDialog(); }
示例 5:讀取外部版本文件
CI 自動生成 version.txt
準備版本文件:
項目發(fā)布后輸出目錄含有 version.txt
內(nèi)容如:
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:統(tǒng)一封裝 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"; }
調(diào)用方式:
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-beta | UI顯示(標題欄、關于框、Label) | 默認最直觀,獲取產(chǎn)品版本,強烈推薦 |
② | Assembly.GetExecutingAssembly().GetName().Version | <AssemblyVersion> | 1.2.0.0 | 內(nèi)部模塊依賴、調(diào)試 | 獲取程序集綁定版本,不一定展示給用戶 |
③ | FileVersionInfo.FileVersion | <FileVersion> | 1.2.3.0 | 狀態(tài)欄、日志、故障排查 | Windows 文件屬性中可見的“文件版本” |
④ | FileVersionInfo.ProductVersion | <InformationalVersion>(或 <Version>) | 1.2.3-beta | 技術支持、版本詳情 | 和 Application.ProductVersion 一致 |
⑤ | 讀取 version.txt、嵌入資源等 | CI/CD 或 Git 自動生成 | 1.2.3+g123abc | 內(nèi)部構建版本控制 | 靈活但需配合構建腳本或 CI 工具 |
⑥ | 自定義 AboutBox 顯示 | 可組合 ①~⑤ | 自由定制 | 標準“關于”窗口 | 常用于商業(yè)軟件,集中展示版本、版權等 |
推薦選擇指南
開發(fā)初期快速顯示:使用 Application.ProductVersion
需要對比程序集版本綁定:使用 AssemblyVersion
需要展示文件詳細版本(如系統(tǒng)托盤右鍵):使用 FileVersionInfo
需要區(qū)分構建版本(多環(huán)境發(fā)布):結合 CI 寫入 version.txt
面向最終用戶展示:統(tǒng)一寫入 AboutBox,使用封裝工具類讀取版本
以上就是.Net Winform開發(fā)顯示程序版本號的常見方式的詳細內(nèi)容,更多關于.Net顯示程序版本號的資料請關注腳本之家其它相關文章!
相關文章
c#遍歷System.drawing.Color下面的所有顏色以及名稱以查看
c#遍歷System.drawing.Color下面的所有顏色以及名稱以查看,需要的朋友可以參考一下2013-02-02C#實現(xiàn)根據(jù)字節(jié)數(shù)截取字符串并加上省略號的方法
這篇文章主要介紹了C#實現(xiàn)根據(jù)字節(jié)數(shù)截取字符串并加上省略號的方法,比較實用的功能,需要的朋友可以參考下2014-07-07C#中使用jieba.NET、WordCloudSharp制作詞云圖的步驟
之前一篇文章介紹的是使用Python的jieba、wordcloud的庫生成詞云圖,本文則介紹在C#中如何使用jieba.NET、WordCloudSharp庫生成詞云圖,感興趣的朋友一起看看吧2021-07-07C#中System.Array.CopyTo() 和 System.Array.Clon()&nbs
System.Array.CopyTo()和System.Array.Clone()是用于數(shù)組復制的兩種不同方法,本文就來介紹C,#中System.Array.CopyTo() 和 System.Array.Clon() 的區(qū)別,具有一定的參考價值,感興趣的可以了解一下2024-04-04