您的位置:首页 > 编程语言 > C#

【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自动生成的代码

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
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: