C#获取程序集的版本号和最后编译时间
C#获取程序集的版本号:
string ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
C#中如何将记录项目的最后编译时间:
在实际的软件开发工作中,我们通常需要记录某个工程的最后编译时间,原来在C++中,我们有个__DATE__,__TIME__,__FILE__,__LINE__这样的异性宏定义可以使用,但是在C#中,不能使用,但是可以用以下语句来获得最后编译时间。
System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location)
出处:http://tangzhongxin.blog.163.com/blog/static/89219612010113173146319
==========================================================================
We write all those code repetitively for dynamic assembly loading and checking to verify few properties on assemblies. It would be a great stop to write all such things in the assemblyinfo.cs (because it needs to completely describe the assembly that it is intended to serve for) You can define your About form of the application entirely using the AssemblyInfo.cs
How to use the following info:
AssemblyInfo ainfo = new AssemblyInfo();
frmAbout.Text = ainfo.Title;
frmAbout.menuAbt.Text = string.Format("&About{0}..",ainfo.Title);
frmAbout.Text = "About " + this.Owner.Text;
frmAbout.Icon = this.Owner.Icon;
//You can set the icon like this on the abt form.
frmAbout.pictureBox1.Image = this.Owner.Icon.ToBitmap();
frmAbout.lblTitle.Text = ainfo.Title;
frmAbout.lblVersion.Text = ainfo.Version;
frmAbout.lblCopyright.Text = ainfo.Copyright;
frmAbout.lblDescription.Text = ainfo.Description;
frmAbout.lblCodebase.Text = ainfo.CodeBase;
private void Form1_Load(object sender, EventArgs e) { AssemblyInfo ainfo = new AssemblyInfo(typeof(Form1)); textBox1.Text += Environment.NewLine + ainfo.AssemblyName; textBox1.Text += Environment.NewLine + ainfo.AssemblyFullName; textBox1.Text += Environment.NewLine + ainfo.CodeBase; textBox1.Text += Environment.NewLine + ainfo.Copyright; textBox1.Text += Environment.NewLine + ainfo.Company; textBox1.Text += Environment.NewLine + ainfo.Description; textBox1.Text += Environment.NewLine + ainfo.Product; textBox1.Text += Environment.NewLine + ainfo.Title; textBox1.Text += Environment.NewLine + ainfo.Version; textBox1.Text += Environment.NewLine + ainfo.FileVersion; textBox1.Text += Environment.NewLine + System.IO.File.GetLastWriteTime(ainfo.Location); }
下面是具体的实现代码。
using System.Reflection; using System; using System.Runtime.InteropServices; // 有关程序集的一般信息由以下 // 控制。更改这些特性值可修改 // 与程序集关联的信息。 [assembly: AssemblyTitle("AssemblyTitle--321")] [assembly: AssemblyDescription("AssemblyDescription")] [assembly: AssemblyConfiguration("AssemblyConfiguration")] [assembly: AssemblyCompany("Microsoft")] [assembly: AssemblyProduct("AssemblyProduct")] [assembly: AssemblyCopyright("Copyright © Microsoft 2020")] [assembly: AssemblyTrademark("AssemblyTrademark")] [assembly: AssemblyCulture("")] [assembly: AssemblyAlgorithmId(System.Configuration.Assemblies.AssemblyHashAlgorithm.MD5)] // 将 ComVisible 设置为 false 会使此程序集中的类型 //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 //请将此类型的 ComVisible 特性设置为 true。 [assembly: ComVisible(false)] // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID [assembly: Guid("66e39086-eb9c-4b52-a6d2-3c029640e5b6")] // 程序集的版本信息由下列四个值组成: // // 主版本 // 次版本 // 生成号 // 修订号 // // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 // 方法是按如下所示使用“*”: : // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.0.0.1")] [assembly: AssemblyFileVersion("1.0.0.321")] #region "Class to get the information for actual form" /********** * This class uses the System.Reflection.Assembly class to access assembly meta-data. * This class is not a normal feature of AssmblyInfo.cs **********/ /// <summary> /// AssemblyInfo class. /// </summary> public class AssemblyInfo { //Used by functions to access information from Assembly Attributes /// <summary> /// myType. /// </summary> private Type myType; /// <summary> /// Initializes a new instance of the <see cref="AssemblyInfo"/> class. /// </summary> /// <param name="typeofValue">typeofValue here denotes the actual form.</param> public AssemblyInfo(Type typeofValue) { //myType = typeofValue; myType = typeof(AssemblyInfo); } /// <summary> /// Gets the name of the assembly. /// </summary> /// <value>The name of the assembly.</value> public String AssemblyName { get { return myType.Assembly.GetName().Name.ToString(); } } /// <summary> /// Gets the full name of the assembly. /// </summary> /// <value>The full name of the assembly.</value> public String AssemblyFullName { get { return myType.Assembly.GetName().FullName.ToString(); } } /// <summary> /// Gets the code base. /// </summary> /// <value>The code base.</value> public String CodeBase { get { return myType.Assembly.CodeBase; } } /// <summary> /// Gets the copyright. /// </summary> /// <value>The copyright.</value> public String Copyright { get { Type att = typeof(AssemblyCopyrightAttribute); object[] r = myType.Assembly.GetCustomAttributes(att, false); AssemblyCopyrightAttribute copyattr = (AssemblyCopyrightAttribute)r[0]; return copyattr.Copyright; } } /// <summary> /// Gets the company. /// </summary> /// <value>The company.</value> public String Company { get { Type att = typeof(AssemblyCompanyAttribute); object[] r = myType.Assembly.GetCustomAttributes(att, false); AssemblyCompanyAttribute compattr = (AssemblyCompanyAttribute)r[0]; return compattr.Company; } } /// <summary> /// Gets the description. /// </summary> /// <value>The description.</value> public String Description { get { Type att = typeof(AssemblyDescriptionAttribute); object[] r = myType.Assembly.GetCustomAttributes(att, false); AssemblyDescriptionAttribute descattr = (AssemblyDescriptionAttribute)r[0]; return descattr.Description; } } /// <summary> /// Gets the product. /// </summary> /// <value>The product.</value> public String Product { get { Type att = typeof(AssemblyProductAttribute); object[] r = myType.Assembly.GetCustomAttributes(att, false); AssemblyProductAttribute prodattr = (AssemblyProductAttribute)r[0]; return prodattr.Product; } } /// <summary> /// Gets the title. /// </summary> /// <value>The title.</value> public String Title { get { Type att = typeof(AssemblyTitleAttribute); object[] r = myType.Assembly.GetCustomAttributes(att, false); AssemblyTitleAttribute titleattr = (AssemblyTitleAttribute)r[0]; return titleattr.Title; } } /// <summary> /// Gets the version. /// </summary> /// <value>The version.</value> public String Version { get { return myType.Assembly.GetName().Version.ToString(); } } public String FileVersion { get { Type att = typeof(AssemblyFileVersionAttribute); object[] r = myType.Assembly.GetCustomAttributes(att, false); AssemblyFileVersionAttribute titleattr = (AssemblyFileVersionAttribute)r[0]; return titleattr.Version; } } public String Location { get { return myType.Assembly.Location; } } } #endregion
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/3904629.html
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!
posted on 2014-08-11 14:10 jack_Meng 阅读(5033) 评论(0) 编辑 收藏 举报