反向思考回去,既然可以取得到資料夾檔案,那只要他有取得到該檔案,就可以寫成一個Boolean型別的方法,將其值回傳
透過DirectoryInfo的GetFiles(String)去取得到資料夾底下的所有檔案,並將上述話實做成一個Boolean method
透過if去判斷是否存在該檔案,下面是我的一個示範
當我要找『Fedora-16-x86_64-DVD.iso』檔案,在GetFiles參數內輸入『Fedora-16-x86_64-DVD.iso』,執行後就知道是否有該檔案存在
該資料夾確定有Fedora-16-x86_64-DVD.iso
我接著找『a.txt』就不存在啦
using System;
using System.IO;
using System.Windows.Forms;
namespace GetFileDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (GetFile())
{
MessageBox.Show("有找到");
}
else
{
MessageBox.Show("沒有找到");
}
}
private Boolean GetFile()
{
DirectoryInfo dirInfo = new DirectoryInfo(@"E:\");
foreach (FileInfo info in dirInfo.GetFiles("Fedora-16-x86_64-DVD.iso"))
{
return true;
}
return false;
}
}
}
參考資料:
http://msdn.microsoft.com/zh-tw/library/system.io.directoryinfo.aspx


