.NET SDK-Style 專案(Core、Standard、.NET5)中的版本號
獨立觀察員 2020 年 12 月 24 日
之前 .NET Framework 時,專案版本號等資訊是存盤在 AssemblyInfo.cs 檔案中,通過程式集特性進行設定:

.NET Core 之后,.NET 專案采用了新式的 SDK-Style 模式,將這些版本資訊之類的也包含在專案檔案里了,默認不再生成和使用 AssemblyInfo.cs 檔案,而且如果你將這個檔案添加上并填寫相關資訊,會提示有重復,編譯不通過,雖然也有方法來恢復以前使用 AssemblyInfo.cs 的方式,但正所謂入鄉隨俗,既然人家改了模式,還是按規范來吧,
圖形操作上和以前差不多,在 屬性 - 打包 中有 “包版本”、“程式集版本” 和 “程式集檔案版本”:

編輯后就會在專案檔案中出現,專案檔案可通過在專案上右鍵 - 編輯專案檔案 打開(此操作也是 SDK-Style 的特色):

具體資訊就是生成在 .csproj 的 PropertyGroup 節點內:

程式集版本(AssemblyVersion)和以前一樣(也支持通配符 *),包版本(Version)對應以前的程式集資訊版本(AssemblyInformationalVersion),程式集檔案版本(FileVersion)對應以前的(AssemblyFileVersion):

另外,這里是在 WPF 中系結了程式集版本資訊,方法如下,也就是引入命名空間和使用:

AssemblyHelper 如下:
using System.Reflection; /* * 原始碼己托管:http://gitee.com/dlgcy/dotnetcodes */ namespace DotNet.Utilities { /// <summary> /// 程式集幫助類 /// </summary> public class AssemblyHelper { #region 程式集特性訪問器 /// <summary> /// 程式集標題 /// </summary> public static 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); } } /// <summary> /// 程式集版本 /// </summary> public static string AssemblyVersion { get { return Assembly.GetExecutingAssembly().GetName().Version.ToString(); } } /// <summary> /// 程式集清單的其他版本資訊 /// </summary> public static string AssemblyInformationalVersion { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyInformationalVersionAttribute)attributes[0]).InformationalVersion; } } /// <summary> /// Win32 檔案版本 /// </summary> public static string AssemblyFileVersion { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyFileVersionAttribute)attributes[0]).Version; } } /// <summary> /// 程式集的文本說明 /// </summary> public static string AssemblyDescription { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyDescriptionAttribute)attributes[0]).Description; } } /// <summary> /// 程式集清單的產品名 /// </summary> public static string AssemblyProduct { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyProductAttribute)attributes[0]).Product; } } /// <summary> /// 程式集清單的著作權 /// </summary> public static string AssemblyCopyright { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyCopyrightAttribute)attributes[0]).Copyright; } } /// <summary> /// 程式集清單的公司名稱 /// </summary> public static string AssemblyCompany { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyCompanyAttribute)attributes[0]).Company; } } #endregion } }
這個和以前是通用的,
最后祝大家平安夜快樂!
原創文章,轉載請注明: 轉載自 獨立觀察員?博客
本文鏈接地址: .NET SDK-Style 專案(Core、Standard、.NET5)中的版本號 [http://dlgcy.com/dotnet-sdk-style-version/]
微信公眾號
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/239849.html
標籤:.NET Core
上一篇:c#求代碼求助
下一篇:區塊鏈騙局-礦機模式之資金盤
