[C#] 常用工具类——应用程序属性信息访问类
using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespace Utils { /// <summary> /// <para> </para> /// 常用工具类——应用程序属性信息访问类 /// <para> -------------------------------------------</para> /// <para> GetAssemblyTitle:获取应用程序集的标题</para> /// <para> GetAssemblyProduct:获取应用程序产品名称</para> /// <para> GetAssemblyVersion:获取应用程序版本</para> /// <para> GetAssemblyDescription:获取应用程序说明</para> /// <para> GetAssemblyCopyright:获取应用程序版权信息</para> /// <para> GetAssemblyCompany:获取应用程序公司名称</para> /// <para> GetAssemblyAppFullName:获取应用程序显示名称</para> /// </summary> public class AssemblyHelper { #region 获取应用程序集的标题 /// <summary> /// 获取应用程序集的标题 /// </summary> /// <returns></returns> public static string GetAssemblyTitle() { 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); } #endregion #region 获取应用程序产品名称 /// <summary> /// 获取应用程序产品名称 /// </summary> /// <returns></returns> public static string GetAssemblyProduct() { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyProductAttribute)attributes[0]).Product; } #endregion #region 获取应用程序版本 /// <summary> /// 获取应用程序版本 /// </summary> /// <returns></returns> public static string GetAssemblyVersion() { return Assembly.GetExecutingAssembly().GetName().Version.ToString(); } #endregion #region 获取应用程序说明 /// <summary> /// 获取应用程序说明 /// </summary> /// <returns></returns> public static string GetAssemblyDescription() { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyDescriptionAttribute)attributes[0]).Description; } #endregion #region 获取应用程序版权信息 /// <summary> /// 获取应用程序版权信息 /// </summary> /// <returns></returns> public static string GetAssemblyCopyright() { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyCopyrightAttribute)attributes[0]).Copyright; } #endregion #region 获取应用程序公司名称 /// <summary> /// 获取应用程序公司名称 /// </summary> /// <returns></returns> public static string GetAssemblyCompany() { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyCompanyAttribute)attributes[0]).Company; } #endregion #region 获取应用程序显示名称 /// <summary> /// 获取应用程序显示名称 /// </summary> /// <returns></returns> public static string GetAssemblyAppFullName() { return Assembly.GetExecutingAssembly().FullName.ToString(); } #endregion } }
作者:阿笨
【官方QQ一群:跟着阿笨一起玩NET(已满)】:422315558
【官方QQ二群:跟着阿笨一起玩C#(已满)】:574187616
【官方QQ三群:跟着阿笨一起玩ASP.NET(已满)】:967920586
【官方QQ四群:Asp.Net Core跨平台技术开发(可加入)】:829227829
【官方QQ五群:.NET Core跨平台开发技术(可加入)】:647639415
【网易云课堂】:https://study.163.com/provider/2544628/index.htm?share=2&shareId=2544628
【腾讯课堂】:https://abennet.ke.qq.com
【51CTO学院】:https://edu.51cto.com/sd/66c64
【微信公众号】:微信搜索:跟着阿笨一起玩NET