You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 9控制台应用如何获取Application.ProductName与ProductVersion?

.NET 9控制台应用获取产品名称和版本的方法

在WinForms中Application.ProductName和Application.ProductVersion本质是读取程序集的元数据,在.NET 9控制台应用里可以通过System.Reflection.Assembly类来获取对应信息,以下是两种常用方式:

  • 获取入口程序集的信息(对应启动的主程序集)

    using System.Reflection;
    
    var entryAssembly = Assembly.GetEntryAssembly();
    var productName = entryAssembly?.GetCustomAttribute<AssemblyProductAttribute>()?.Product;
    var productVersion = entryAssembly?.GetName().Version?.ToString();
    
    var appName = $"{productName}:{productVersion}";
    
  • 获取当前代码所在程序集的信息(适用于类库等非入口程序集场景)

    using System.Reflection;
    
    var executingAssembly = Assembly.GetExecutingAssembly();
    var productName = executingAssembly?.GetCustomAttribute<AssemblyProductAttribute>()?.Product;
    var productVersion = executingAssembly?.GetName().Version?.ToString();
    
    var appName = $"{productName}:{productVersion}";
    

补充说明

上述获取的ProductName对应项目文件(.csproj)中的<Product>配置项,ProductVersion对应<Version>配置项,你可以直接在项目文件中设置:

<PropertyGroup>
  <Product>你的应用名称</Product>
  <Version>1.0.0.0</Version>
</PropertyGroup>

内容的提问来源于stack exchange,提问作者ZedZip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 02:52:04