依目錄、檔名尋找出符合的檔案清單
前言
依目錄、檔名尋找符合的檔案清單,最常用就是使用For Each的方式,但是也可以使用LINQ來找哦!
實作
範例說明
- Search Dir中有部門的目錄。
- 部門:Dept_1~Dept_5。
- 每個部門目錄中有2009.txt & 2010.txt
- 可在要找的部門欄位中輸入要找的目錄名稱,多個以逗號隔開(空字串表示不篩選,全部出來)。
- 可在要找的檔案欄位中輸入要找的檔案名稱,多個以逗號隔開(空字串表示不篩選,全部出來)。
1.使用For Each
1: /// <summary>2: /// 使用ForEach來找檔案3: /// </summary>4: /// <param name="sender"></param>5: /// <param name="e"></param>6: /// <remarks>7: /// 當查詢清單為空表示全部查出來8: /// </remarks>9: private void btnSearchForEach_Click(object sender, EventArgs e)10: {
11: string startFolder = txtSearchDir.Text;12: string[] aryFolderName = Directory.GetDirectories(startFolder);13: char[] splitChar = new char[] { ',' };14: //要找的部門清單15: string[] searchDeptList = txtDeptList.Text.Trim().Split(splitChar);16: //要找的檔案清單17: string[] searchFileList = txtFileList.Text.Trim().Split(splitChar);18: txtSearchResult.Text = string.Empty;19: foreach (string strFolder in aryFolderName)20: {
21: if (txtDeptList.Text.Trim().Length > 0)22: {
23: //有設定部門篩選24: foreach (string strFilterFolder in searchDeptList)25: {
26: if (strFolder == Path.Combine(startFolder, strFilterFolder))27: {
28: //有符合的Folder29: string[] aryFileList = Directory.GetFiles(strFolder);30: foreach (string strFile in aryFileList)31: {
32: if (txtFileList.Text.Trim().Length > 0)33: {
34: //有設定檔案篩選35: foreach (string strFilterFile in searchFileList)36: {
37: if (strFilterFile.ToUpper() == Path.GetFileName(strFile).ToUpper())38: {
39: txtSearchResult.Text += strFile + Environment.NewLine;
40: }
41: }
42: }
43: else44: {
45: //沒有設定檔案篩選46: txtSearchResult.Text += strFile + Environment.NewLine;
47: }
48:
49: }
50: }
51: }
52: }
53: else54: {
55: //沒有設定部門篩選56: string[] aryFileList = Directory.GetFiles(strFolder);57: foreach (string strFile in aryFileList)58: {
59: if (txtFileList.Text.Trim().Length > 0)60: {
61: //有設定檔案篩選62: foreach (string strFilterFile in searchFileList)63: {
64: if (strFilterFile.ToUpper() == Path.GetFileName(strFile).ToUpper())65: {
66: txtSearchResult.Text += strFile + Environment.NewLine;
67: }
68: }
69: }
70: else71: {
72: //沒有設定檔案篩選73: txtSearchResult.Text += strFile + Environment.NewLine;
74: }
75:
76: }
77: }
78:
79: }
80:
81: }
2.使用LINQ
1: /// <summary>2: /// 使用LINQ來找Files3: /// </summary>4: /// <param name="sender"></param>5: /// <param name="e"></param>6: /// <remarks>7: /// 當查詢清單為空表示全部查出來8: /// </remarks>9: private void btnSearch_Click(object sender, EventArgs e)10: {
11: string startFolder = txtSearchDir.Text;12: // Take a snapshot of the file system.13: System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);14:
15: // This method assumes that the application has discovery permissions16: // for all folders under the specified path.17: IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);18:
19: char[] splitChar = new char[]{','};20:
21: //要找的部門清單22: string[] searchDeptList = txtDeptList.Text.Trim().Split(splitChar);23: //要找的檔案清單24: string[] searchFileList = txtFileList.Text.Trim().Split(splitChar);25:
26: //Create the query 如果清單為空,就找出全部的資料27: IEnumerable<System.IO.FileInfo> fileQuery =
28: from file in fileList29: from d in searchDeptList30: where file.DirectoryName == Path.Combine(startFolder, d) || d == string.Empty31: from f in searchFileList32: where file.Name.ToUpper() == f.ToUpper() || f == string.Empty33: orderby file.DirectoryName
34: select file;
35:
36:
37: txtSearchResult.Text = string.Empty;38: //Execute the query. This might write out a lot of files!39: foreach (System.IO.FileInfo fi in fileQuery)40: {
41: txtSearchResult.Text += fi.FullName + Environment.NewLine;
42: }
43: }
結論
使用LINQ來找似乎比較直覺一點,而在篩選資料時,把要篩選的Folder Name 及 File Name當成另外一個資料來源,跟全部的Files一起Join,同時加上當篩選的資料為空值時,就不篩選(全部出來),所以就不能直接使用join,而要使用form where的方式! 如下,
IEnumerable
fileQuery =
from file in fileList
from d in searchDeptList
where file.DirectoryName == Path.Combine(startFolder, d) || d == string.Empty
from f in searchFileList
where file.Name.ToUpper() == f.ToUpper() || f == string.Empty
orderby file.DirectoryName
select file;
參考資訊
範例
Hi,
亂馬客Blog已移到了 「亂馬客 : Re:從零開始的軟體開發生活」
請大家繼續支持 ^_^