See 1:
public static string[] GetDirectories2(string dir, string regexPattern = null, int depth = 0, bool throwEx = false) { List<string> lst = new List<string>(); try { foreach (string item in Directory.GetDirectories(dir)) { try { if (regexPattern != null && item.Contains(regexPattern)) { lst.Add(item); } //递归 if (depth != 0) { lst.AddRange(GetDirectories2(item, regexPattern, depth - 1, throwEx)); } } catch { if (throwEx) { throw; } } } } catch { if (throwEx) { throw; } } return lst.ToArray(); }
调用:DirUtil.GetDirectories2(rootDir, "Log_PayLogUnWrite", 6);
Referring to 2, this method is the first match is returned, there is a need to modify their own:
/// <the Summary> /// Find specify the name of the folder, the default depth of 6 /// </ the Summary> /// <name = "path" param> path're looking for </ param> /// <param name = "searchDirName"> files to find a folder name </ param> /// <param name = "depth"> Find depth </ param> /// <returns a> </ returns a> List < String > FindDirPath ( String path, String searchDirName, int depth = . 6 ) { List < String >lst = new List<string>(); try { string[] dirs = Directory.GetDirectories(path); foreach (string dir in dirs) { if (dir.Contains("$RECYCLE.BIN") || dir.Contains("System Volume Information")) continue; string dirName = dir.Substring(dir.LastIndexOf("\\") + 1); if (dirName.ToLower().EndsWith("temp")) // This procedure needs to be skipped temp folder Continue ; IF (dirName == searchDirName) { lst.Add (the dir); BREAK ; // first match is returned } // recursive IF (depth =! 0 ) { lst.AddRange (FindDirPath (the dir, searchDirName)); } } } the catch (Exception EX) { UILog ( " FindDirPath1 EX: " + ex.Message); } return lst; }
调用 :List<string> lst = FindDirPath("d:\\", Folder);