目錄
項目概述
主要組件及功能
類定義
控件聲明
構造函數
Dispose 方法
InitializeComponents 方法
控件配置詳解
TextBox 控件 (textBox1)
GroupBox 控件 (groupBox1)
Label 控件 (label1?至?label5)
OpenFileDialog 控件 (openFileDialog1)
Label 控件 (label6)
窗體配置
事件處理方法
項目截圖:
項目完整源碼:
項目概述
這是一個簡單的C# Windows Forms應用程序,展示了如何通過用戶界面與文件系統進行交互,并利用.NET框架提供的API來獲取文件的元數據,用于瀏覽和獲取程序文件的信息。具體功能包括:
- 瀏覽程序文件:允許用戶通過文件對話框選擇一個
.exe
文件。 - 獲取并顯示程序文件信息:一旦選擇了文件,應用程序會讀取并顯示該文件的詳細信息,如公司名稱、產品名稱、語言標志、版本號和版權聲明。
- 獲取當前程序文件信息:還可以顯示當前運行的應用程序本身的詳細信息。
- 創建窗體和控件:
- 定義窗體類?
Form1
?并添加各種控件(按鈕、文本框、分組框、標簽等)。 - 在?
InitializeComponent
?方法中配置每個控件的位置、大小、文本和其他屬性。
- 定義窗體類?
- 事件綁定:
- 為每個按鈕綁定點擊事件處理方法。
button1_Click
: 打開文件對話框并顯示選中的文件路徑。button2_Click
: 獲取并顯示所選文件的詳細信息。button3_Click
: 獲取并顯示所選的應用程序本身的詳細信息。
主要組件及功能
類定義
public class Form1 : System.Windows.Forms.Form
Form1
繼承自System.Windows.Forms.Form
,是整個應用程序的主要窗體類。
控件聲明
private Button button1; private TextBox textBox1; private GroupBox groupBox1; private OpenFileDialog openFileDialog1; private Button button2; private Label label1; private Label label2; private Label label3; private Label label4; private Label label5; private Label label6; private Button button3;
- Button: 提供用戶交互按鈕。
- TextBox: 顯示選定文件路徑。
- GroupBox: 包含多個標簽用于顯示文件信息。
- OpenFileDialog: 文件打開對話框,用于讓用戶選擇文件。
- Label: 標簽控件,用于顯示文本信息。
構造函數
public Form1() {InitializeComponent(); }
- 初始化窗體及其所有子控件。
Dispose 方法
protected override void Dispose(bool disposing) {if (disposing && components != null){components.Dispose();}base.Dispose(disposing); }
- 清理所有正在使用的資源。
InitializeComponents 方法
此方法由Visual Studio設計器生成,用于初始化窗體上的各個控件及其屬性。
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.label5 = new System.Windows.Forms.Label();this.label4 = new System.Windows.Forms.Label();this.label3 = new System.Windows.Forms.Label();this.label2 = new System.Windows.Forms.Label();this.label1 = new System.Windows.Forms.Label();this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();this.button2 = new System.Windows.Forms.Button();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(41, 48);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(276, 112);this.textBox1.Name = "textBox1";this.textBox1.ReadOnly = true;this.textBox1.Size = new System.Drawing.Size(256, 21);this.textBox1.TabIndex = 2;// // groupBox1// this.groupBox1.Controls.Add(this.label5);this.groupBox1.Controls.Add(this.label4);this.groupBox1.Controls.Add(this.label3);this.groupBox1.Controls.Add(this.label2);this.groupBox1.Controls.Add(this.label1);this.groupBox1.Location = new System.Drawing.Point(41, 187);this.groupBox1.Name = "groupBox1";this.groupBox1.Size = new System.Drawing.Size(602, 236);this.groupBox1.TabIndex = 3;this.groupBox1.TabStop = false;this.groupBox1.Text = "程序文件信息";// // label5// this.label5.Location = new System.Drawing.Point(28, 197);this.label5.Name = "label5";this.label5.Size = new System.Drawing.Size(320, 16);this.label5.TabIndex = 4;this.label5.Text = "版權聲明:";// // label4// this.label4.Location = new System.Drawing.Point(28, 160);this.label4.Name = "label4";this.label4.Size = new System.Drawing.Size(320, 16);this.label4.TabIndex = 3;this.label4.Text = "版本號:";// // label3// this.label3.Location = new System.Drawing.Point(28, 125);this.label3.Name = "label3";this.label3.Size = new System.Drawing.Size(320, 16);this.label3.TabIndex = 2;this.label3.Text = "語言標志:";// // label2// this.label2.Location = new System.Drawing.Point(28, 81);this.label2.Name = "label2";this.label2.Size = new System.Drawing.Size(320, 16);this.label2.TabIndex = 1;this.label2.Text = "產品名稱:";// // label1// this.label1.Location = new System.Drawing.Point(28, 28);this.label1.Name = "label1";this.label1.Size = new System.Drawing.Size(320, 16);this.label1.TabIndex = 0;this.label1.Text = "公司名稱:";// // openFileDialog1// this.openFileDialog1.Filter = "程序文件(*.exe)|*.exe|All files (*.*)|*.*";// // button2// this.button2.Location = new System.Drawing.Point(277, 48);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);// // label6// this.label6.Location = new System.Drawing.Point(130, 117);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(515, 48);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(683, 450);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);this.PerformLayout();}
控件配置詳解
Button 控件 (button1
)
this.button1.Location = new System.Drawing.Point(41, 48); 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);
- Location: 設置按鈕的位置為?
(41, 48)
。 - Name: 按鈕的名稱為?
button1
。 - Size: 按鈕的大小為?
112x23
?像素。 - TabIndex: 按鈕的Tab順序索引為?
1
。 - Text: 按鈕上顯示的文字為“瀏覽程序文件”。
- Click Event: 綁定點擊事件到?
button1_Click
?方法。
TextBox 控件 (textBox1
)
this.textBox1.BackColor = System.Drawing.SystemColors.Control; this.textBox1.Location = new System.Drawing.Point(276, 112); this.textBox1.Name = "textBox1"; this.textBox1.ReadOnly = true; this.textBox1.Size = new System.Drawing.Size(256, 21); this.textBox1.TabIndex = 2;
- BackColor: 文本框背景顏色為系統控制顏色。
- Location: 設置文本框的位置為?
(276, 112)
。 - Name: 文本框的名稱為?
textBox1
。 - ReadOnly: 文本框設置為只讀模式。
- Size: 文本框的大小為?
256x21
?像素。 - TabIndex: 文本框的Tab順序索引為?
2
。
GroupBox 控件 (groupBox1
)
this.groupBox1.Controls.Add(this.label5); this.groupBox1.Controls.Add(this.label4); this.groupBox1.Controls.Add(this.label3); this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.label1); this.groupBox1.Location = new System.Drawing.Point(41, 187); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(602, 236); this.groupBox1.TabIndex = 3; this.groupBox1.TabStop = false; this.groupBox1.Text = "程序文件信息";
- Controls: 將?
label1
?到?label5
?添加到?groupBox1
?中。 - Location: 設置?
groupBox1
?的位置為?(41, 187)
。 - Name:?
groupBox1
?的名稱為?groupBox1
。 - Size:?
groupBox1
?的大小為?602x236
?像素。 - TabIndex:?
groupBox1
?的Tab順序索引為?3
。 - TabStop: 設置為?
false
?表示不作為Tab順序的一部分。 - Text:?
groupBox1
?上顯示的文字為“程序文件信息”。
Label 控件 (label1
?至?label5
)
這些標簽用于顯示具體的文件信息。
// 示例:label1 this.label1.Location = new System.Drawing.Point(28, 28); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(320, 16); this.label1.TabIndex = 0; this.label1.Text = "公司名稱:";
- Location: 設置標簽的位置。
- Name: 標簽的名稱。
- Size: 標簽的大小。
- TabIndex: 標簽的Tab順序索引。
- Text: 標簽上顯示的文字。
OpenFileDialog 控件 (openFileDialog1
)
this.openFileDialog1.Filter = "程序文件(*.exe)|*.exe|All files (*.*)|*.*";
- Filter: 設置文件過濾器,僅顯示?
.exe
?文件和所有文件。
Button 控件 (button2
)
this.button2.Location = new System.Drawing.Point(277, 48); 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);
- 同樣設置了位置、名稱、大小、Tab順序和文字,并綁定了點擊事件到?
button2_Click
?方法。
Label 控件 (label6
)
this.label6.Location = new System.Drawing.Point(130, 117); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(96, 16); this.label6.TabIndex = 12; this.label6.Text = "程序文件名稱:";
- 同樣設置了位置、名稱、大小、Tab順序和文字。
Button 控件 (button3
)
this.button3.Location = new System.Drawing.Point(515, 48); 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);
- 同樣設置了位置、名稱、大小、Tab順序和文字,并綁定了點擊事件到?
button3_Click
?方法。
窗體配置
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(683, 450); 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 = "演示獲取程序文件信息";
- AutoScaleBaseSize: 設置自動縮放基準大小。
- ClientSize: 設置客戶端區域的大小。
- Controls: 添加所有控件到窗體中。
- MaximizeBox: 設置不允許最大化窗口。
- Name: 窗體的名稱為?
Form1
。 - StartPosition: 設置窗體啟動時居中顯示。
- Text: 窗體標題欄顯示的文字為“演示獲取程序文件信息”。
事件處理方法
瀏覽程序文件 (button1_Click
)
private void button1_Click(object sender, EventArgs e) {if (openFileDialog1.ShowDialog() == DialogResult.OK){textBox1.Text = openFileDialog1.FileName;} }
- 當點擊“瀏覽程序文件”按鈕時,調用?
openFileDialog1.ShowDialog()
?彈出文件對話框。 - 如果用戶選擇了一個文件并點擊了“確定”,則將選中的文件路徑賦值給?
textBox1.Text
。
獲取程序文件信息 (button2_Click
)
private void button2_Click(object sender, EventArgs e) {string myFileName = textBox1.Text;if (myFileName.Length < 1)return;string shortName = myFileName.Substring(myFileName.LastIndexOf("\\") + 1);groupBox1.Text = shortName + "程序文件信息";FileVersionInfo myInfo = FileVersionInfo.GetVersionInfo(myFileName);label1.Text = "公司名稱:" + myInfo.CompanyName;label2.Text = "產品名稱:" + myInfo.ProductName;label3.Text = "語言標志:" + myInfo.Language;label4.Text = "版本號:" + myInfo.FileVersion;label5.Text = "版權聲明:" + myInfo.LegalCopyright; }
- 從?
textBox1
?中獲取文件路徑存儲在?myFileName
?變量中。 - 如果文件路徑為空,則直接返回。
- 使用?
Substring
?和?LastIndexOf
?方法提取文件名,并設置?groupBox1
?的標題。 - 使用?
FileVersionInfo.GetVersionInfo
?方法獲取文件的版本信息。 - 將版本信息分別賦值給?
label1
?到?label5
?的?Text
?屬性以顯示相關信息。
獲取本程序文件信息 (button3_Click
)
private void button3_Click(object sender, EventArgs e) {groupBox1.Text = "顯示本程序文件信息";label1.Text = "公司名稱:" + Application.CompanyName;label2.Text = "區域信息:" + Application.CurrentCulture;label3.Text = "語言標志:" + Application.CurrentInputLanguage;label4.Text = "產品名稱:" + Application.ProductName;label5.Text = "產品版本:" + Application.ProductVersion; }
- ?直接使用?
Application
?類的靜態屬性獲取當前應用程序的版本信息。 - 將這些信息分別賦值給?
label1
?到?label5
?的?Text
?屬性以顯示相關信息。
項目截圖:
項目完整源碼:
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.label5 = new System.Windows.Forms.Label();this.label4 = new System.Windows.Forms.Label();this.label3 = new System.Windows.Forms.Label();this.label2 = new System.Windows.Forms.Label();this.label1 = new System.Windows.Forms.Label();this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();this.button2 = new System.Windows.Forms.Button();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(41, 48);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(276, 112);this.textBox1.Name = "textBox1";this.textBox1.ReadOnly = true;this.textBox1.Size = new System.Drawing.Size(256, 21);this.textBox1.TabIndex = 2;// // groupBox1// this.groupBox1.Controls.Add(this.label5);this.groupBox1.Controls.Add(this.label4);this.groupBox1.Controls.Add(this.label3);this.groupBox1.Controls.Add(this.label2);this.groupBox1.Controls.Add(this.label1);this.groupBox1.Location = new System.Drawing.Point(41, 187);this.groupBox1.Name = "groupBox1";this.groupBox1.Size = new System.Drawing.Size(602, 236);this.groupBox1.TabIndex = 3;this.groupBox1.TabStop = false;this.groupBox1.Text = "程序文件信息";// // label5// this.label5.Location = new System.Drawing.Point(28, 197);this.label5.Name = "label5";this.label5.Size = new System.Drawing.Size(320, 16);this.label5.TabIndex = 4;this.label5.Text = "版權聲明:";// // label4// this.label4.Location = new System.Drawing.Point(28, 160);this.label4.Name = "label4";this.label4.Size = new System.Drawing.Size(320, 16);this.label4.TabIndex = 3;this.label4.Text = "版本號:";// // label3// this.label3.Location = new System.Drawing.Point(28, 125);this.label3.Name = "label3";this.label3.Size = new System.Drawing.Size(320, 16);this.label3.TabIndex = 2;this.label3.Text = "語言標志:";// // label2// this.label2.Location = new System.Drawing.Point(28, 81);this.label2.Name = "label2";this.label2.Size = new System.Drawing.Size(320, 16);this.label2.TabIndex = 1;this.label2.Text = "產品名稱:";// // label1// this.label1.Location = new System.Drawing.Point(28, 28);this.label1.Name = "label1";this.label1.Size = new System.Drawing.Size(320, 16);this.label1.TabIndex = 0;this.label1.Text = "公司名稱:";// // openFileDialog1// this.openFileDialog1.Filter = "程序文件(*.exe)|*.exe|All files (*.*)|*.*";// // button2// this.button2.Location = new System.Drawing.Point(277, 48);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);// // label6// this.label6.Location = new System.Drawing.Point(130, 117);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(515, 48);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(683, 450);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);this.PerformLayout();}#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; }} }