依目錄、檔名尋找出符合的檔案清單[LINQ]

依目錄、檔名尋找出符合的檔案清單

前言

依目錄、檔名尋找符合的檔案清單,最常用就是使用For Each的方式,但是也可以使用LINQ來找哦!

實作

範例說明

Screen

  • Search Dir中有部門的目錄。
  • 部門:Dept_1~Dept_5。
  • 每個部門目錄中有2009.txt & 2010.txt
  • 可在要找的部門欄位中輸入要找的目錄名稱,多個以逗號隔開(空字串表示不篩選,全部出來)。
  • 可在要找的檔案欄位中輸入要找的檔案名稱,多個以逗號隔開(空字串表示不篩選,全部出來)。

SearchDirTree

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:                      //有符合的Folder
			  29:                      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:                          else
			  44:                          {
			  45:                              //沒有設定檔案篩選
			  46:                              txtSearchResult.Text += strFile + Environment.NewLine;
			  47:                          }
			  48:                          
			  49:                      }
			  50:                  }
			  51:              }
			  52:          }
			  53:          else
			  54:          {
			  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:                  else
			  71:                  {
			  72:                      //沒有設定檔案篩選
			  73:                      txtSearchResult.Text += strFile + Environment.NewLine;
			  74:                  }
			  75:   
			  76:              }
			  77:          }
			  78:          
			  79:      }
			  80:   
			  81:  }

2.使用LINQ

			   1:  /// <summary>
			   2:  /// 使用LINQ來找Files
			   3:  /// </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 permissions
			  16:      // 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 fileList
			  29:          from d in searchDeptList
			  30:          where file.DirectoryName == Path.Combine(startFolder, d) || d == string.Empty 
			  31:          from f in searchFileList
			  32:          where file.Name.ToUpper() == f.ToUpper() || f == string.Empty
			  33:          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;

參考資訊

How to: Query for Files with a Specified Attribute or Name

範例

LinQFiles.rar

Hi, 

亂馬客Blog已移到了 「亂馬客​ : Re:從零開始的軟體開發生活

請大家繼續支持 ^_^