C#递归搜索指定目录下的文件或目录
生活随笔
收集整理的這篇文章主要介紹了
C#递归搜索指定目录下的文件或目录
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
來源:https://www.cnblogs.com/huhangfei/p/5012978.html
誠然可以使用現(xiàn)成的Directory類下的GetFiles、GetDirectories、GetFileSystemEntries這幾個(gè)方法實(shí)現(xiàn)同樣的功能,但請相信我不是蛋疼,原因是這幾個(gè)方法在遇上【System Volume Information】這種目錄時(shí),極有可能會(huì)給你個(gè)拒絕訪問的異常,想跳過都不行。所以沒辦法,重新實(shí)現(xiàn)了一下。 實(shí)現(xiàn)說明- 仍然是基于對Directory類的幾個(gè)方法的封裝進(jìn)行實(shí)現(xiàn),只是沒有使用它們的searchPattern和searchOption功能 - 將匹配模式由windows的通配符?、*改為正則匹配。一是讓匹配更強(qiáng)大,二是要實(shí)現(xiàn)?*匹配還得做額外工作,沒必要匹配模式并沒有默認(rèn)添加首尾限定^$,即“abc"將會(huì)匹配所有包含該字串的項(xiàng)目,所以如果你要匹配首尾,請自行添加^$忽略大小寫匹配如果不想搜索指定項(xiàng)目而是全部,請將regexPattern參數(shù)設(shè)為null,而不是.*,前者性能更好 - 可通過設(shè)置recurse和throwEx參數(shù)決定是否遞歸搜索,和是否拋異常。默認(rèn)是不遞歸,不拋異常 - 遇到不可訪問的目錄會(huì)跳過。當(dāng)然前提是throwEx=false - 之所以在foreach外層再套一層try-catch,是因?yàn)槿绻付ǖ膁ir就是不可訪問的目錄,那也可以避免異常 - 之所以為獲取項(xiàng)、獲取文件、獲取目錄分別實(shí)現(xiàn)3個(gè)方法,而不是只實(shí)現(xiàn)一個(gè)獲取項(xiàng),另外兩個(gè)重載,是因?yàn)橹粚?shí)現(xiàn)一個(gè)的話,foreach中要做的邏輯判斷不少,考慮到方法是要遞歸的,所以循環(huán)中邏輯越少越好 - 之所以不做dir參數(shù)合法性檢查,原因同上,判斷越少越好。所以請用戶調(diào)用前自行確保dir合法不廢話,上代碼:
1 /// <summary> 2 /// 獲取指定目錄中的匹配項(xiàng)(文件或目錄) 3 /// </summary> 4 /// <param name="dir">要搜索的目錄</param> 5 /// <param name="regexPattern">項(xiàng)名模式(正則)。null表示忽略模式匹配,返回所有項(xiàng)</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 }
?
? ?總結(jié)
以上是生活随笔為你收集整理的C#递归搜索指定目录下的文件或目录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux文件删除机制,Linux 文件
- 下一篇: c#多线程操作界面控件的简单实现