Dav*_*vid 5 asp.net-core-mvc asp.net-core-2.0
我正在Win10上使用.net core 2.0和vs2017构建一个mvc web应用程序.在写一个'About'页面时,我看起来放入了当前的项目版本号(目前仍然设置为1.0.0).我原以为这非常简单!
我能找到的唯一参考建议:
AppVersion = typeof(RuntimeEnvironment).GetTypeInfo ().Assembly
.GetCustomAttribute<AssemblyFileVersionAttribute> ().Version;
Run Code Online (Sandbox Code Playgroud)
但是,在我的情况下,这将返回'4.6.25814.01' - 不是必需的.
有人可以建议如何在代码中检索版本吗?
我假设我想要'包版本',但承认我不清楚区分/如何使用'包版本','汇编版本'和'汇编文件版本'.
Sig*_*gge 11
当您调用时typeof(RuntimeEnvironment).Assembly
,您正在查询该类型的包含程序集.在这种情况下,这将是System.Runtime.InteropServices.dll
或Microsoft.Dotnet.PlatformAbstractions.dll
,取决于您导入的命名空间.
例如,要获取自己的程序集的信息,可以简单地替换RuntimeEnvironment
为您自己的类型
var appVersion = typeof(Program).Assembly
.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
Run Code Online (Sandbox Code Playgroud)
甚至
var appVersion = typeof(HomeController).Assembly
.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
Run Code Online (Sandbox Code Playgroud)
如果您的项目设置如下,如果Package版本将返回"6.6.7.0":
你很亲密!
在这里,您可以找到有关.NET反射的更多信息,但它适用于.NET Core.
归档时间: |
|
查看次数: |
3660 次 |
最近记录: |