C#获取程序文件相关信息的方法
程序员文章站
2023-12-19 11:07:46
本文实例讲述了c#获取程序文件相关信息的方法,分享给大家供大家参考。
具体实现方法如下:
using system.reflection;
using sys...
本文实例讲述了c#获取程序文件相关信息的方法,分享给大家供大家参考。
具体实现方法如下:
using system.reflection; using system.runtime.compilerservices; // // 有关程序集的常规信息是通过下列 // 属性集控制的。更改这些属性值可修改与程序集 // 关联的信息。 // [assembly: assemblytitle("")] [assembly: assemblydescription("")] [assembly: assemblyconfiguration("")] [assembly: assemblycompany("")] [assembly: assemblyproduct("")] [assembly: assemblycopyright("")] [assembly: assemblytrademark("")] [assembly: assemblyculture("")] // // 程序集的版本信息由下列 4 个值组成: // // 主版本 // 次版本 // 内部版本号 // 修订号 // // 您可以指定所有这些值,也可以使用“修订号”和“内部版本号”的默认值,方法是按 // 如下所示使用 '*': [assembly: assemblyversion("1.0.*")] // // 要对程序集进行签名,必须指定要使用的密钥。有关程序集签名的更多信息,请参考 // microsoft .net framework 文档。 // // 使用下面的属性控制用于签名的密钥。 // // 注意: // (*) 如果未指定密钥,则程序集不会被签名。 // (*) keyname 是指已经安装在计算机上的 // 加密服务提供程序(csp)中的密钥。keyfile 是指包含 // 密钥的文件。 // (*) 如果 keyfile 和 keyname 值都已指定,则 // 发生下列处理: // (1) 如果在 csp 中可以找到 keyname,则使用该密钥。 // (2) 如果 keyname 不存在而 keyfile 存在,则 // keyfile 中的密钥安装到 csp 中并且使用该密钥。 // (*) 要创建 keyfile,可以使用 sn.exe(强名称)实用工具。 // 在指定 keyfile 时,keyfile 的位置应该相对于 // 项目输出目录,即 // %project directory%obj<configuration>。例如,如果 keyfile 位于 // 该项目目录,应将 assemblykeyfile // 属性指定为 [assembly: assemblykeyfile("..\..\mykey.snk")] // (*) “延迟签名”是一个高级选项 - 有关它的更多信息,请参阅 microsoft .net framework // 文档。 // [assembly: assemblydelaysign(false)] [assembly: assemblykeyfile("")] [assembly: assemblykeyname("")]
forms1.frm文件这里是核心程序
using system; using system.drawing; using system.collections; using system.componentmodel; using system.windows.forms; using system.data; using system.io; using system.diagnostics; namespace myfile { /// <summary> /// form1 的摘要说明。 /// </summary> public class form1 : system.windows.forms.form { private system.windows.forms.button button1; private system.windows.forms.textbox textbox1; private system.windows.forms.groupbox groupbox1; private system.windows.forms.openfiledialog openfiledialog1; private system.windows.forms.button button2; private system.windows.forms.label label1; private system.windows.forms.label label2; private system.windows.forms.label label3; private system.windows.forms.label label4; private system.windows.forms.label label5; private system.windows.forms.label label6; private system.windows.forms.button button3; /// <summary> /// 必需的设计器变量。 /// </summary> private system.componentmodel.container components = null; public form1() { // // windows 窗体设计器支持所必需的 // initializecomponent(); // // todo: 在 initializecomponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void dispose( bool disposing ) { if( disposing ) { if (components != null) { components.dispose(); } } base.dispose( disposing ); } #region windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void initializecomponent() { this.button1 = new system.windows.forms.button(); this.textbox1 = new system.windows.forms.textbox(); this.groupbox1 = new system.windows.forms.groupbox(); this.openfiledialog1 = new system.windows.forms.openfiledialog(); this.button2 = new system.windows.forms.button(); this.label1 = new system.windows.forms.label(); this.label2 = new system.windows.forms.label(); this.label3 = new system.windows.forms.label(); this.label4 = new system.windows.forms.label(); this.label5 = new system.windows.forms.label(); this.label6 = new system.windows.forms.label(); this.button3 = new system.windows.forms.button(); this.groupbox1.suspendlayout(); this.suspendlayout(); // // button1 // this.button1.location = new system.drawing.point(8, 192); this.button1.name = "button1"; this.button1.size = new system.drawing.size(112, 23); this.button1.tabindex = 1; this.button1.text = "浏览程序文件"; this.button1.click += new system.eventhandler(this.button1_click); // // textbox1 // this.textbox1.backcolor = system.drawing.systemcolors.control; this.textbox1.location = new system.drawing.point(104, 8); this.textbox1.name = "textbox1"; this.textbox1.readonly = true; this.textbox1.size = new system.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.jb51.net)s.groupbox1.controls.add(this.label3); this.groupbox1.controls.add(this.label2); this.groupbox1.controls.add(this.label1); this.groupbox1.location = new system.drawing.point(8, 40); this.groupbox1.name = "groupbox1"; this.groupbox1.size = new system.drawing.size(352, 144); this.groupbox1.tabindex = 3; this.groupbox1.tabstop = false; this.groupbox1.text = "程序文件信息"; // // openfiledialog1 // this.openfiledialog1.filter = "程序文件(*.exe)|*.exe|all files (*.*)|*.*"; // // button2 // this.button2.location = new system.drawing.point(120, 192); this.button2.name = "button2"; this.button2.size = new system.drawing.size(112, 23); this.button2.tabindex = 11; this.button2.text = "获取程序文件信息"; this.button2.click += new system.eventhandler(this.button2_click); // // label1 // this.label1.location = new system.drawing.point(16, 24); this.label1.name = "label1"; this.label1.size = new system.drawing.size(320, 16); this.label1.tabindex = 0; this.label1.text = "公司名称:"; // // label2 // this.label2.location = new system.drawing.point(16, 48); this.label2.name = "label2"; this.label2.size = new system.drawing.size(320, 16); this.label2.tabindex = 1; this.label2.text = "产品名称:"; // // label3 // this.label3.location = new system.drawing.point(16, 72); this.label3.name = "label3"; this.label3.size = new system.drawing.size(320, 16); this.label3.tabindex = 2; this.label3.text = "语言标志:"; // // label4 // this.label4.location = new system.drawing.point(16, 96); this.label4.name = "label4"; this.label4.size = new system.drawing.size(320, 16); this.label4.tabindex = 3; this.label4.text = "版本号:"; // // label5 // this.label5.location = new system.drawing.point(16, 120); this.label5.name = "label5"; this.label5.size = new system.drawing.size(320, 16); this.label5.tabindex = 4; this.label5.text = "版权声明:"; // // label6 // this.label6.location = new system.drawing.point(8, 16); this.label6.name = "label6"; this.label6.size = new system.drawing.size(96, 16); this.label6.tabindex = 12; this.label6.text = "程序文件名称:"; // // button3 // this.button3.location = new system.drawing.point(232, 192); this.button3.name = "button3"; this.button3.size = new system.drawing.size(128, 23); this.button3.tabindex = 13; this.button3.text = "获取本程序文件信息"; this.button3.click += new system.eventhandler(this.button3_click); // // form1 // this.autoscalebasesize = new system.drawing.size(6, 14); this.clientsize = new system.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] static void main() { application.run(new form1()); } private void button1_click(object sender, system.eventargs e) {//浏览程序文件 if(this.openfiledialog1.showdialog()==dialogresult.ok) { this.textbox1.text=this.openfiledialog1.filename; } } private void button2_click(object sender, system.eventargs e) {//获取程序文件信息 string myfilename=this.textbox1.text; if(myfilename.length<1) return; string shortname=myfilename.substring(myfilename.lastindexof("\")+1); this.groupbox1.text=shortname+"程序文件信息"; fileversioninfo myinfo=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; } private void button3_click(object sender, system.eventargs e) {//获取当前程序文件信息 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#程序设计有所帮助。