欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.Net?Winform開發(fā)顯示程序版本號的常見方式

 更新時間:2025年05月13日 16:03:38   作者:dotnet研習社  
在?WinForms?桌面應用程序開發(fā)中,向用戶顯示當前程序的版本號是一個常見的需求,本文將介紹在?WinForms?應用中顯示程序版本號的幾種常見方式,大家可以了解下

歡迎關注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

示例代碼:

在窗體中添加了 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
  • 適用于:狀態(tài)欄、底部信息區(qū)。

示例 4:AboutBox 顯示版本號

使用 Application.ProductVersion

添加步驟:

在窗體中添加了 menuStriptoolStripMenuItem 控件,命名為 menuStrip1toolStripMenuItem1。

  • 添加 → 新建項 → “關于框(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-betaUI顯示(標題欄、關于框、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顯示程序版本號的資料請關注腳本之家其它相關文章!

相關文章

最新評論