C#获取程序文件相关信息的方法
本文实例讲述了C#获取程序文件相关信息的方法,分享给大家供大家参考。
具体实现方法如下:
usingSystem.Reflection; usingSystem.Runtime.CompilerServices; // //有关程序集的常规信息是通过下列 //属性集控制的。更改这些属性值可修改与程序集 //关联的信息。 // [assembly:AssemblyTitle("")] [assembly:AssemblyDescription("")] [assembly:AssemblyConfiguration("")] [assembly:AssemblyCompany("")] [assembly:AssemblyProduct("")] [assembly:AssemblyCopyright("")] [assembly:AssemblyTrademark("")] [assembly:AssemblyCulture("")] // //程序集的版本信息由下列4个值组成: // //主版本 //次版本 //内部版本号 //修订号 // //您可以指定所有这些值,也可以使用“修订号”和“内部版本号”的默认值,方法是按 //如下所示使用'*': [assembly:AssemblyVersion("1.0.*")] // //要对程序集进行签名,必须指定要使用的密钥。有关程序集签名的更多信息,请参考 //Microsoft.NETFramework文档。 // //使用下面的属性控制用于签名的密钥。 // //注意: //(*)如果未指定密钥,则程序集不会被签名。 //(*)KeyName是指已经安装在计算机上的 //加密服务提供程序(CSP)中的密钥。KeyFile是指包含 //密钥的文件。 //(*)如果KeyFile和KeyName值都已指定,则 //发生下列处理: //(1)如果在CSP中可以找到KeyName,则使用该密钥。 //(2)如果KeyName不存在而KeyFile存在,则 //KeyFile中的密钥安装到CSP中并且使用该密钥。 //(*)要创建KeyFile,可以使用sn.exe(强名称)实用工具。 //在指定KeyFile时,KeyFile的位置应该相对于 //项目输出目录,即 //%ProjectDirectory%obj<configuration>。例如,如果KeyFile位于 //该项目目录,应将AssemblyKeyFile //属性指定为[assembly:AssemblyKeyFile("..\..\mykey.snk")] //(*)“延迟签名”是一个高级选项-有关它的更多信息,请参阅Microsoft.NETFramework //文档。 // [assembly:AssemblyDelaySign(false)] [assembly:AssemblyKeyFile("")] [assembly:AssemblyKeyName("")]
forms1.frm文件这里是核心程序
usingSystem; usingSystem.Drawing; usingSystem.Collections; usingSystem.ComponentModel; usingSystem.Windows.Forms; usingSystem.Data; usingSystem.IO; usingSystem.Diagnostics; namespaceMyFile { ///<summary> ///Form1的摘要说明。 ///</summary> publicclassForm1:System.Windows.Forms.Form { privateSystem.Windows.Forms.Buttonbutton1; privateSystem.Windows.Forms.TextBoxtextBox1; privateSystem.Windows.Forms.GroupBoxgroupBox1; privateSystem.Windows.Forms.OpenFileDialogopenFileDialog1; privateSystem.Windows.Forms.Buttonbutton2; privateSystem.Windows.Forms.Labellabel1; privateSystem.Windows.Forms.Labellabel2; privateSystem.Windows.Forms.Labellabel3; privateSystem.Windows.Forms.Labellabel4; privateSystem.Windows.Forms.Labellabel5; privateSystem.Windows.Forms.Labellabel6; privateSystem.Windows.Forms.Buttonbutton3; ///<summary> ///必需的设计器变量。 ///</summary> privateSystem.ComponentModel.Containercomponents=null; publicForm1() { // //Windows窗体设计器支持所必需的 // InitializeComponent(); // //TODO:在InitializeComponent调用后添加任何构造函数代码 // } ///<summary> ///清理所有正在使用的资源。 ///</summary> protectedoverridevoidDispose(booldisposing) { if(disposing) { if(components!=null) { components.Dispose(); } } base.Dispose(disposing); } #regionWindows窗体设计器生成的代码 ///<summary> ///设计器支持所需的方法-不要使用代码编辑器修改 ///此方法的内容。 ///</summary> privatevoidInitializeComponent() { this.button1=newSystem.Windows.Forms.Button(); this.textBox1=newSystem.Windows.Forms.TextBox(); this.groupBox1=newSystem.Windows.Forms.GroupBox(); this.openFileDialog1=newSystem.Windows.Forms.OpenFileDialog(); this.button2=newSystem.Windows.Forms.Button(); this.label1=newSystem.Windows.Forms.Label(); this.label2=newSystem.Windows.Forms.Label(); this.label3=newSystem.Windows.Forms.Label(); this.label4=newSystem.Windows.Forms.Label(); this.label5=newSystem.Windows.Forms.Label(); this.label6=newSystem.Windows.Forms.Label(); this.button3=newSystem.Windows.Forms.Button(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // //button1 // this.button1.Location=newSystem.Drawing.Point(8,192); this.button1.Name="button1"; this.button1.Size=newSystem.Drawing.Size(112,23); this.button1.TabIndex=1; this.button1.Text="浏览程序文件"; this.button1.Click+=newSystem.EventHandler(this.button1_Click); // //textBox1 // this.textBox1.BackColor=System.Drawing.SystemColors.Control; this.textBox1.Location=newSystem.Drawing.Point(104,8); this.textBox1.Name="textBox1"; this.textBox1.ReadOnly=true; this.textBox1.Size=newSystem.Drawing.Size(256,21); this.textBox1.TabIndex=2; this.textBox1.Text=""; // //groupBox1 // this.groupBox1.Controls.Add(this.label5); this.groupBox1.Controls.Add(this.label4); thi(www.nhooo.com)s.groupBox1.Controls.Add(this.label3); this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.label1); this.groupBox1.Location=newSystem.Drawing.Point(8,40); this.groupBox1.Name="groupBox1"; this.groupBox1.Size=newSystem.Drawing.Size(352,144); this.groupBox1.TabIndex=3; this.groupBox1.TabStop=false; this.groupBox1.Text="程序文件信息"; // //openFileDialog1 // this.openFileDialog1.Filter="程序文件(*.exe)|*.exe|Allfiles(*.*)|*.*"; // //button2 // this.button2.Location=newSystem.Drawing.Point(120,192); this.button2.Name="button2"; this.button2.Size=newSystem.Drawing.Size(112,23); this.button2.TabIndex=11; this.button2.Text="获取程序文件信息"; this.button2.Click+=newSystem.EventHandler(this.button2_Click); // //label1 // this.label1.Location=newSystem.Drawing.Point(16,24); this.label1.Name="label1"; this.label1.Size=newSystem.Drawing.Size(320,16); this.label1.TabIndex=0; this.label1.Text="公司名称:"; // //label2 // this.label2.Location=newSystem.Drawing.Point(16,48); this.label2.Name="label2"; this.label2.Size=newSystem.Drawing.Size(320,16); this.label2.TabIndex=1; this.label2.Text="产品名称:"; // //label3 // this.label3.Location=newSystem.Drawing.Point(16,72); this.label3.Name="label3"; this.label3.Size=newSystem.Drawing.Size(320,16); this.label3.TabIndex=2; this.label3.Text="语言标志:"; // //label4 // this.label4.Location=newSystem.Drawing.Point(16,96); this.label4.Name="label4"; this.label4.Size=newSystem.Drawing.Size(320,16); this.label4.TabIndex=3; this.label4.Text="版本号:"; // //label5 // this.label5.Location=newSystem.Drawing.Point(16,120); this.label5.Name="label5"; this.label5.Size=newSystem.Drawing.Size(320,16); this.label5.TabIndex=4; this.label5.Text="版权声明:"; // //label6 // this.label6.Location=newSystem.Drawing.Point(8,16); this.label6.Name="label6"; this.label6.Size=newSystem.Drawing.Size(96,16); this.label6.TabIndex=12; this.label6.Text="程序文件名称:"; // //button3 // this.button3.Location=newSystem.Drawing.Point(232,192); this.button3.Name="button3"; this.button3.Size=newSystem.Drawing.Size(128,23); this.button3.TabIndex=13; this.button3.Text="获取本程序文件信息"; this.button3.Click+=newSystem.EventHandler(this.button3_Click); // //Form1 // this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14); this.ClientSize=newSystem.Drawing.Size(368,222); this.Controls.Add(this.button3); this.Controls.Add(this.label6); this.Controls.Add(this.groupBox1); this.Controls.Add(this.textBox1); this.Controls.Add(this.button1); this.Controls.Add(this.button2); this.MaximizeBox=false; this.Name="Form1"; this.StartPosition=System.Windows.Forms.FormStartPosition.CenterScreen; this.Text="演示获取程序文件信息"; this.groupBox1.ResumeLayout(false); this.ResumeLayout(false); } #endregion ///<summary> ///应用程序的主入口点。 ///</summary> [STAThread] staticvoidMain() { Application.Run(newForm1()); } privatevoidbutton1_Click(objectsender,System.EventArgse) {//浏览程序文件 if(this.openFileDialog1.ShowDialog()==DialogResult.OK) { this.textBox1.Text=this.openFileDialog1.FileName; } } privatevoidbutton2_Click(objectsender,System.EventArgse) {//获取程序文件信息 stringMyFileName=this.textBox1.Text; if(MyFileName.Length<1) return; stringShortName=MyFileName.Substring(MyFileName.LastIndexOf("\")+1); this.groupBox1.Text=ShortName+"程序文件信息"; FileVersionInfoMyInfo=FileVersionInfo.GetVersionInfo(MyFileName); this.label1.Text="公司名称:"+MyInfo.CompanyName; this.label2.Text="产品名称:"+MyInfo.ProductName; this.label3.Text="语言标志:"+MyInfo.Language; this.label4.Text="版本号:"+MyInfo.FileVersion; this.label5.Text="版权声明:"+MyInfo.LegalCopyright; } privatevoidbutton3_Click(objectsender,System.EventArgse) {//获取当前程序文件信息 this.groupBox1.Text="显示本程序文件信息"; this.label1.Text="公司名称:"+Application.CompanyName; this.label2.Text="区域信息:"+Application.CurrentCulture; this.label3.Text="语言标志:"+Application.CurrentInputLanguage; this.label4.Text="产品名称:"+Application.ProductName; this.label5.Text="产品版本:"+Application.ProductVersion; } } }
希望本文所述对大家的C#程序设计有所帮助。