【C#】获取程序中Properties下的AssemblyInfo.cs下的信息
2017-08-22 15:23
441 查看
一般C#程序会自动生成AssemblyInfo.cs文件,文件中包含着与此程序相关的程序集信息
,那么在程序中应该如何读取这些信息呢?
// 有关程序集的常规信息通过下列属性集
// 控制。更改这些属性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("XXXXX")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PlateRecognition")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 使此程序集中的类型
// 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型,
// 则将该类型上的 ComVisible 属性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("b165cd1b-d9b5-4e69-9ca5-accdff888dc")]
// 程序集的版本信息由下面四个值组成:
//
// 主版本
// 次版本
// 内部版本号
// 修订号
//
// 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值,
// 方法是按如下所示使用“*”:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
如何读取呢?
最简便的方法,为程序加一个AboutBox窗口,右键-》新建项目-》“关于”框,
这个AboutBox.cs中有自动生成代码,很容易就读取到AssemblyInfo中的信息。
下面贴一下AboutBox.cs自动生成的代码
,那么在程序中应该如何读取这些信息呢?
// 有关程序集的常规信息通过下列属性集
// 控制。更改这些属性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("XXXXX")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PlateRecognition")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 使此程序集中的类型
// 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型,
// 则将该类型上的 ComVisible 属性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("b165cd1b-d9b5-4e69-9ca5-accdff888dc")]
// 程序集的版本信息由下面四个值组成:
//
// 主版本
// 次版本
// 内部版本号
// 修订号
//
// 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值,
// 方法是按如下所示使用“*”:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
如何读取呢?
最简便的方法,为程序加一个AboutBox窗口,右键-》新建项目-》“关于”框,
这个AboutBox.cs中有自动生成代码,很容易就读取到AssemblyInfo中的信息。
下面贴一下AboutBox.cs自动生成的代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Linq; using System.Reflection; using System.Windows.Forms; namespace XXX { partial class AboutBox : Form { public AboutBox() { InitializeComponent(); this.Text = String.Format("关于 {0} {0}", AssemblyTitle); this.labelProductName.Text = AssemblyProduct; this.labelVersion.Text = String.Format("版本 {0} {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 } }
相关文章推荐
- C# AssemblyInfo.cs控制EXE程序版本信息
- asp.net获取程序集信息(AssemblyInfo)
- VS中实时获取SVN的版本号并写入到AssemblyInfo.cs中(C#)
- AssemblyInfo.cs文件:包含程序版本、信息、版权的属性文件(转转转)
- asp.net C#获取程序文件相关信息
- [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute
- [C#] 获取计算机内部信息 - ComputerInfoHelper
- [C#.Net]利用HttpRequest登录到某个网站,然后获取网站信息的程序示例
- C# WinFrom(CS)程序调用 WEB 站点,获取响应内容
- C#使用单例模式类获取本地程序目录下XML文件中的配置信息
- C#获取程序文件相关信息的方法
- C# WinFrom(CS)程序调用 WEB 站点,获取响应内容
- [转]wx.getUserInfo(OBJECT) 微信小程序 获取用户信息
- 微信小程序wx.getImageInfo()如何获取图片信息
- C#微信小程序服务端获取用户解密信息
- asp.net C#获取程序文件相关信息
- C#获取用户系统信息的程序
- C# AssemblyInfo.cs文件的作用
- 在C#中如何获取系统中所安装的所有软件信息,还有如何打开系统中某个程序
- 【微信小程序】用户首次进入小程序拒绝授权,如何再次调用授权页面,获取用户信息userInfo