3-1 Fileinfo類的常用方法
- 2020 年 1 月 6 日
- 筆記
1. 案例學習:了解FileInfo類的一些主要屬性
下面的示例演示了 FileInfo類的一些主要屬性。
using System; using System.IO; class Test { public static void Main() { string fileName = "C:\autoexec.bat"; FileInfo fileInfo = new FileInfo(fileName); if (!fileInfo.Exists) { return; } Console.WriteLine("{0} has a directoryName of {1}",fileName,fileInfo.DirectoryName); /* 下面是程式碼的處理結果, * 實際的結果因機器不同: * * C:autoexec.bat has a directoryName of C: */ } } |
---|
2.案例學習:實現文件的複製
本案例將解決,同磁碟環境下文件複製的問題。請嘗試把C:WinNTWin.INI文件拷貝到C:下的程式碼,怎麼寫呢?
u實驗步驟(1):
向一個Form窗體上拖拽三個Button控制項,三個控制項的text屬性分別設置為「複製文本文件」、「創建文本文件」、 「刪除文本文件」。如圖3-3所示:

圖3-3 文件操作介面圖
u實驗步驟(2):
雙擊「複製文本文件」、「創建文本文件」、「刪除文本文件」,在click事件處理方法里分別添加程式碼如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace FileOptionApplication { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 複製文本文件 /// </summary> private void button1_Click(object sender, EventArgs e) { string somefile = @"C:Documents and SettingsAdministratorMy DocumentsSQL Server2000安裝故障.txt"; string target = @"c:2.txt"; if (!File.Exists(somefile)) { MessageBox.Show("文件不存在!"); } else { if (File.Exists(target)) { File.Delete(target); } File.Copy(somefile, target); MessageBox.Show("文件複製成功!"); } } /// <summary> /// 創建文本文件 /// </summary> private void button2_Click(object sender, EventArgs e) { string target = @"c:2.txt"; if (File.Exists(target)) { File.Delete(target); } File.CreateText(target); } /// <summary> /// 刪除文本文件 /// </summary> private void button3_Click(object sender, EventArgs e) { string target = @"c:2.txt"; if (File.Exists(target)) { File.Delete(target); MessageBox.Show("文件刪除成功!"); } } } } |
---|
問題討論:
剛才的實驗我們是通過File類實現並完成任務的,那麼此次我們通過更換FileInfo類執行同樣的複製動作如何實現呢?請將button1_Click的程式碼替換為下列程式碼試試:
private void button1_Click(object sender, EventArgs e) { string path = @"C:WINDOWSIE4 Error Log.txt"; string target = @"c:1.txt"; FileInfo myfile = new FileInfo(path); if (!myfile.Exists) { MessageBox.Show("對不起,未發現路徑文件!"); } else { myfile.CopyTo(target); MessageBox.Show("複製成功!"); } } |
---|
3.案例學習:獲取文件基本資訊
本案例將解決,如何顯示文件的基本資訊問題。
u實驗步驟(1):
向一個Form窗體上拖拽三個Lable控制項和一個Button控制項,Button控制項的text屬性設置為「獲取文件資訊」。如圖3-4所示:

圖3-4 獲取文件資訊介面圖
u實驗步驟(2):
雙擊「獲取文件資訊」,在click事件處理方法里分別添加程式碼如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace FileOptionApplication { public partial class Form2 : Form { public Form2() { InitializeComponent(); } /// <summary> /// 獲取文件資訊單擊事件 /// </summary> private void button1_Click(object sender, EventArgs e) { string somefile = @"C:Documents and SettingsAdministratorMy DocumentsSQL Server2000安裝故障.txt"; FileInfo myfile = new FileInfo(somefile); if (myfile.Exists) { MessageBox.Show("文件已經存在"); label1.Text = "文件創建時間:" + myfile.CreationTime.ToString(); label2.Text = "文件夾:" + myfile.Directory.ToString(); label3.Text = "文件夾名稱:" + myfile.DirectoryName.ToString() + ",文件擴展名:" + myfile.Extension.ToString(); } else { MessageBox.Show("文件並不存在"); } } } } |
---|
問題討論:
FileInfo類和File類都可以實現上述操作,它們的方法也都非常相似,那麼它們到底有什麼區別呢?
nFileInfo類和File類的比較
n兩者都提供對文件類似的操作。
nFile為靜態類,直接使用;FileInfo需要實例化後才能使用 。
n從性能上考慮,如果你要多次操作文件,不管是針對相同的,還是不同的,請使用FileInfo,說白了,單打獨鬥File最棒,群毆則首推FileInfo。
n每次通過File類調用某個方法時,都要佔用一定的CPU,而FileInfo類只在創建FileInfo對象時執行一次安全檢查。