1.前言
在指定的目錄中,利用程式快速查詢包含特定副檔名或是檔案中含有某關鍵字。
2.說明
加入命名空間
using System.Text.RegularExpressions; using System.IO;
程式碼:
private void FindFile(string dir) { //在指定目錄下查詢文件,若符合查詢條件,將檔案寫入lsFile控制元件 DirectoryInfo Dir = new DirectoryInfo(dir); try { foreach (DirectoryInfo d in Dir.GetDirectories())//查詢子目錄 { FindFile(Dir + d.ToString() + "\\"); } foreach (FileInfo f in Dir.GetFiles("*.xls"))//查詢附檔名為xls的文件 { Regex regex = new Regex(@"friend");//查詢檔案名稱中有關鍵字friend的文件 Match m = regex.Match(f.ToString()); if (m.Success == true) { lsFile.Items.Add(Dir + f.ToString()); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } Application.DoEvents(); }
文章標籤
全站熱搜