來源:https://www.cnblogs.com/huhangfei/p/5012978.html
誠然可以使用現成的Directory類下的GetFiles、GetDirectories、GetFileSystemEntries這幾個方法實現同樣的功能,但請相信我不是蛋疼,原因是這幾個方法在遇上【System Volume Information】這種目錄時,極有可能會給你個拒絕訪問的異常,想跳過都不行。所以沒辦法,重新實現了一下。 實現說明- 仍然是基于對Directory類的幾個方法的封裝進行實現,只是沒有使用它們的searchPattern和searchOption功能 - 將匹配模式由windows的通配符?、*改為正則匹配。一是讓匹配更強大,二是要實現?*匹配還得做額外工作,沒必要匹配模式并沒有默認添加首尾限定^$,即“abc"將會匹配所有包含該字串的項目,所以如果你要匹配首尾,請自行添加^$忽略大小寫匹配如果不想搜索指定項目而是全部,請將regexPattern參數設為null,而不是.*,前者性能更好 - 可通過設置recurse和throwEx參數決定是否遞歸搜索,和是否拋異常。默認是不遞歸,不拋異常 - 遇到不可訪問的目錄會跳過。當然前提是throwEx=false - 之所以在foreach外層再套一層try-catch,是因為如果指定的dir就是不可訪問的目錄,那也可以避免異常 - 之所以為獲取項、獲取文件、獲取目錄分別實現3個方法,而不是只實現一個獲取項,另外兩個重載,是因為只實現一個的話,foreach中要做的邏輯判斷不少,考慮到方法是要遞歸的,所以循環中邏輯越少越好 - 之所以不做dir參數合法性檢查,原因同上,判斷越少越好。所以請用戶調用前自行確保dir合法不廢話,上代碼:
1 /// <summary> 2 /// 獲取指定目錄中的匹配項(文件或目錄) 3 /// </summary> 4 /// <param name="dir">要搜索的目錄</param> 5 /// <param name="regexPattern">項名模式(正則)。null表示忽略模式匹配,返回所有項</param> 6 /// <param name="recurse">是否搜索子目錄</param> 7 /// <param name="throwEx">是否拋異常</param> 8 /// <returns></returns> 9 public static string[] GetFileSystemEntries(string dir, string regexPattern, bool recurse, bool throwEx) 10 { 11 List<string> lst = new List<string>(); 12 13 try 14 { 15 foreach (string item in Directory.GetFileSystemEntries(dir)) 16 { 17 try 18 { 19 if (regexPattern == null || Regex.IsMatch(Path.GetFileName(item), regexPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline)) 20 { lst.Add(item); } 21 22 //遞歸 23 if (recurse && (File.GetAttributes(item) & FileAttributes.Directory) == FileAttributes.Directory) 24 { lst.AddRange(GetFileSystemEntries(item, regexPattern, recurse, throwEx)); } 25 } 26 catch { if (throwEx) { throw; } } 27 } 28 } 29 catch { if (throwEx) { throw; } } 30 31 return lst.ToArray(); 32 } 33 34 /// <summary> 35 /// 獲取指定目錄中的匹配文件 36 /// </summary> 37 /// <param name="dir">要搜索的目錄</param> 38 /// <param name="regexPattern">文件名模式(正則)。null表示忽略模式匹配,返回所有文件</param> 39 /// <param name="recurse">是否搜索子目錄</param> 40 /// <param name="throwEx">是否拋異常</param> 41 /// <returns></returns> 42 public static string[] GetFiles(string dir, string regexPattern, bool recurse, bool throwEx) 43 { 44 List<string> lst = new List<string>(); 45 46 try 47 { 48 foreach (string item in Directory.GetFileSystemEntries(dir)) 49 { 50 try 51 { 52 bool isFile = (File.GetAttributes(item) & FileAttributes.Directory) != FileAttributes.Directory; 53 54 if (isFile && (regexPattern == null || Regex.IsMatch(Path.GetFileName(item), regexPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline))) 55 { lst.Add(item); } 56 57 //遞歸 58 if (recurse && !isFile) { lst.AddRange(GetFiles(item, regexPattern, recurse, throwEx)); } 59 } 60 catch { if (throwEx) { throw; } } 61 } 62 } 63 catch { if (throwEx) { throw; } } 64 65 return lst.ToArray(); 66 } 67 68 /// <summary> 69 /// 獲取指定目錄中的匹配目錄 70 /// </summary> 71 /// <param name="dir">要搜索的目錄</param> 72 /// <param name="regexPattern">目錄名模式(正則)。null表示忽略模式匹配,返回所有目錄</param> 73 /// <param name="recurse">是否搜索子目錄</param> 74 /// <param name="throwEx">是否拋異常</param> 75 /// <returns></returns> 76 public static string[] GetDirectories(string dir, string regexPattern, bool recurse, bool throwEx) 77 { 78 List<string> lst = new List<string>(); 79 80 try 81 { 82 foreach (string item in Directory.GetDirectories(dir)) 83 { 84 try 85 { 86 if (regexPattern == null || Regex.IsMatch(Path.GetFileName(item), regexPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline)) 87 { lst.Add(item); } 88 89 //遞歸 90 if (recurse) { lst.AddRange(GetDirectories(item, regexPattern, recurse, throwEx)); } 91 } 92 catch { if (throwEx) { throw; } } 93 } 94 } 95 catch { if (throwEx) { throw; } } 96 97 return lst.ToArray(); 98 }
?
?
?