.Net?Winform開發(fā)顯示程序版本號的常見方式
歡迎關(guān)注dotnet研習(xí)社,今天我們討論一個Winform開發(fā)中的一個常見的需求內(nèi)容“關(guān)于程序的版本號顯示”。
在 WinForms 桌面應(yīng)用程序開發(fā)中,向用戶顯示當(dāng)前程序的版本號是一個常見的需求,尤其是在產(chǎn)品發(fā)布、更新提示或技術(shù)支持場景中尤為重要。在.NET 8 中已全面采用 SDK 風(fēng)格項目,相比舊的 .NET Framework 項目,版本號的設(shè)置和讀取方式更加規(guī)范和現(xiàn)代化。本文將介紹在 WinForms 應(yīng)用中顯示程序版本號的幾種常見方式,并附上示例代碼,供大家參考和選擇。
項目準(zhǔn)備
確保我們的 .csproj 是 SDK 風(fēng)格,并配置版本號:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<!-- 版本信息設(shè)置 -->
<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:窗體標(biāo)題欄顯示版本號
使用 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。
- 添加 → 新建項 → “關(guān)于框(About Box)”
- 在
AboutBox1.cs修改版本號設(shè)置:
partial class AboutBox1 : Form
{
public AboutBox1()
{
InitializeComponent();
this.Text = String.Format("關(guān)于 {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
準(zhǔn)備版本文件:
項目發(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 = $"構(gòu)建版本:{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);

對比總結(jié)
| 方式編號 | 獲取方式 | 來源(csproj 或程序集) | 示例輸出 | 推薦用途 | 特點說明 |
|---|---|---|---|---|---|
| ① | Application.ProductVersion | <InformationalVersion>(或 <Version>) | 1.2.3-beta | UI顯示(標(biāo)題欄、關(guān)于框、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 | 技術(shù)支持、版本詳情 | 和 Application.ProductVersion 一致 |
| ⑤ | 讀取 version.txt、嵌入資源等 | CI/CD 或 Git 自動生成 | 1.2.3+g123abc | 內(nèi)部構(gòu)建版本控制 | 靈活但需配合構(gòu)建腳本或 CI 工具 |
| ⑥ | 自定義 AboutBox 顯示 | 可組合 ①~⑤ | 自由定制 | 標(biāo)準(zhǔn)“關(guān)于”窗口 | 常用于商業(yè)軟件,集中展示版本、版權(quán)等 |
推薦選擇指南
開發(fā)初期快速顯示:使用 Application.ProductVersion
需要對比程序集版本綁定:使用 AssemblyVersion
需要展示文件詳細版本(如系統(tǒng)托盤右鍵):使用 FileVersionInfo
需要區(qū)分構(gòu)建版本(多環(huán)境發(fā)布):結(jié)合 CI 寫入 version.txt
面向最終用戶展示:統(tǒng)一寫入 AboutBox,使用封裝工具類讀取版本
以上就是.Net Winform開發(fā)顯示程序版本號的常見方式的詳細內(nèi)容,更多關(guān)于.Net顯示程序版本號的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#程序?qū)崿F(xiàn)將MySQL的存儲過程轉(zhuǎn)換成Oracle的存儲過程
文章介紹了如何使用C#控制臺應(yīng)用,將MySQL自增ID和批量插入語句轉(zhuǎn)換為Oracle兼容的SEQUENCE、觸發(fā)器和INSERTALL語法,實現(xiàn)存儲過程自動適配,提升數(shù)據(jù)庫遷移效率,需要的朋友可以參考下2025-10-10
c# 數(shù)據(jù)類型占用的字節(jié)數(shù)介紹
本篇文章主要是對c#中數(shù)據(jù)類型占用的字節(jié)數(shù)進行了詳細的介紹。需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01
C#操作SQLite數(shù)據(jù)庫方法小結(jié)
這篇文章介紹了C#操作SQLite數(shù)據(jù)庫的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
C#編程實現(xiàn)CMD定時關(guān)機的示例代碼
本文使用C#編程調(diào)用Windows的CMD命令提示符實現(xiàn)定時關(guān)機功能,通過System.Diagnostics.Process類創(chuàng)建CMD進程并執(zhí)行定時關(guān)機命令,下面就來詳細的介紹一下,感興趣的可以了解一下2025-12-12
基于C#的圖表控件庫 ScottPlot編譯visual studio 2022
基于 C# 的 圖表控件庫 ScottPlot,開源免費,可以用于開發(fā)一些上位機軟件,如電壓、電流波形的顯示,開發(fā)【示波器】圖形界面,可以顯示一些圖表、波形,總之功能比較的強大,本文介紹了基于C#的圖表控件庫 ScottPlot編譯visual studio 2022,需要的朋友可以參考下2022-06-06
C#使用OpenCvSharp4實現(xiàn)讀取本地視頻
OpenCvSharp4庫是一個基于.Net封裝的OpenCV庫,這篇文章主要介紹了C#使用OpenCvSharp4實現(xiàn)讀取本地視頻的詳細教程,有需要的小伙伴可以參考下2024-01-01
C#使用iTextSharp庫將圖片轉(zhuǎn)換為PDF
iTextSharp 是一個開源的 .NET 庫,主要用于創(chuàng)建和操作 PDF 文檔,本文主要介紹了如何使用 C# 和 iTextSharp 將圖片轉(zhuǎn)換為 PDF 的功能,需要的可以參考下2024-12-12

