/// <summary> /// Returns a list of all files found in the given folder. /// The search is recursive. /// </summary> /// <param name="rootpath">The folder to look in</param> /// <param name="callback">The function to call with the filenames</param> /// <param name="folderList">A function to call that lists all folders in the supplied folder</param> /// <param name="fileList">A function to call that lists all files in the supplied folder</param> /// <param name="attributeReader">A function to call that obtains the attributes for an element, set to null to avoid reading attributes</param> /// <returns>A list of the full filenames</returns> public static IEnumerable <string> EnumerateFileSystemEntries(string rootpath, EnumerationFilterDelegate callback, FileSystemInteraction folderList, FileSystemInteraction fileList, ExtractFileAttributes attributeReader) { Stack <string> lst = new Stack <string>(); var isFolder = false; try { if (attributeReader == null) { isFolder = true; } else { isFolder = (attributeReader(rootpath) & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory; } } catch { } if (isFolder) { rootpath = AppendDirSeparator(rootpath); try { System.IO.FileAttributes attr = attributeReader == null ? System.IO.FileAttributes.Directory : attributeReader(rootpath); if (callback(rootpath, rootpath, attr)) { lst.Push(rootpath); } } catch (System.Threading.ThreadAbortException) { throw; } catch (Exception) { callback(rootpath, rootpath, ATTRIBUTE_ERROR | System.IO.FileAttributes.Directory); } while (lst.Count > 0) { string f = AppendDirSeparator(lst.Pop()); yield return(f); try { foreach (string s in folderList(f)) { var sf = AppendDirSeparator(s); System.IO.FileAttributes attr = attributeReader == null ? System.IO.FileAttributes.Directory : attributeReader(sf); if (callback(rootpath, sf, attr)) { lst.Push(sf); } } } catch (System.Threading.ThreadAbortException) { throw; } catch (Exception) { callback(rootpath, f, ATTRIBUTE_ERROR | System.IO.FileAttributes.Directory); } string[] files = null; if (fileList != null) { try { files = fileList(f); } catch (System.Threading.ThreadAbortException) { throw; } }
/// <summary> /// Returns a list of all files found in the given folder. /// The search is recursive. /// </summary> /// <param name="rootpath">The folder to look in</param> /// <param name="callback">The function to call with the filenames</param> /// <param name="folderList">A function to call that lists all folders in the supplied folder</param> /// <param name="fileList">A function to call that lists all files in the supplied folder</param> /// <returns>A list of the full filenames</returns> public static IEnumerable <string> EnumerateFileSystemEntries(string rootpath, EnumerationFilterDelegate callback, FileSystemInteraction folderList, FileSystemInteraction fileList) { return(EnumerateFileSystemEntries(rootpath, callback, folderList, fileList, null)); }
/// <summary> /// Returns a list of all files found in the given folder. /// The search is recursive. /// </summary> /// <param name="rootpath">The folder to look in</param> /// <param name="filter">An optional filter to apply to the filenames</param> /// <param name="callback">The function to call with the filenames</param> /// <returns>A list of the full filenames</returns> public static IEnumerable <string> EnumerateFileSystemEntries(string rootpath, EnumerationFilterDelegate callback) { return(EnumerateFileSystemEntries(rootpath, callback, new FileSystemInteraction(System.IO.Directory.GetDirectories), new FileSystemInteraction(System.IO.Directory.GetFiles))); }
/// <summary> /// Returns a list of all files found in the given folder. /// The search is recursive. /// </summary> /// <param name="rootpath">The folder to look in</param> /// <param name="callback">The function to call with the filenames</param> /// <param name="folderList">A function to call that lists all folders in the supplied folder</param> /// <param name="fileList">A function to call that lists all files in the supplied folder</param> /// <param name="attributeReader">A function to call that obtains the attributes for an element, set to null to avoid reading attributes</param> /// <returns>A list of the full filenames</returns> public static IEnumerable<string> EnumerateFileSystemEntries(string rootpath, EnumerationFilterDelegate callback, FileSystemInteraction folderList, FileSystemInteraction fileList, ExtractFileAttributes attributeReader) { Stack<string> lst = new Stack<string>(); var isFolder = false; try { if (attributeReader == null) isFolder = true; else isFolder = (attributeReader(rootpath) & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory; } catch { } if (isFolder) { rootpath = AppendDirSeparator(rootpath); try { System.IO.FileAttributes attr = attributeReader == null ? System.IO.FileAttributes.Directory : attributeReader(rootpath); if (callback(rootpath, rootpath, attr)) lst.Push(rootpath); } catch (System.Threading.ThreadAbortException) { throw; } catch (Exception) { callback(rootpath, rootpath, ATTRIBUTE_ERROR | System.IO.FileAttributes.Directory); } while (lst.Count > 0) { string f = AppendDirSeparator(lst.Pop()); yield return f; try { foreach(string s in folderList(f)) { var sf = AppendDirSeparator(s); System.IO.FileAttributes attr = attributeReader == null ? System.IO.FileAttributes.Directory : attributeReader(sf); if (callback(rootpath, sf, attr)) lst.Push(sf); } } catch (System.Threading.ThreadAbortException) { throw; } catch (Exception) { callback(rootpath, f, ATTRIBUTE_ERROR | System.IO.FileAttributes.Directory); } string[] files = null; if (fileList != null) try { files = fileList(f); } catch (System.Threading.ThreadAbortException) { throw; } catch (Exception) { callback(rootpath, f, ATTRIBUTE_ERROR); } if (files != null) foreach(var s in files) { try { System.IO.FileAttributes attr = attributeReader == null ? System.IO.FileAttributes.Normal : attributeReader(s); if (!callback(rootpath, s, attr)) continue; } catch (System.Threading.ThreadAbortException) { throw; } catch (Exception) { callback(rootpath, s, ATTRIBUTE_ERROR); continue; } yield return s; } } } else { try { System.IO.FileAttributes attr = attributeReader == null ? System.IO.FileAttributes.Normal : attributeReader(rootpath); if (!callback(rootpath, rootpath, attr)) yield break; } catch (System.Threading.ThreadAbortException) { throw; } catch (Exception) { callback(rootpath, rootpath, ATTRIBUTE_ERROR); yield break; } yield return rootpath; } }
/// <summary> /// Returns a list of all files found in the given folder. /// The search is recursive. /// </summary> /// <param name="rootpath">The folder to look in</param> /// <param name="callback">The function to call with the filenames</param> /// <param name="folderList">A function to call that lists all folders in the supplied folder</param> /// <param name="fileList">A function to call that lists all files in the supplied folder</param> /// <returns>A list of the full filenames</returns> public static IEnumerable<string> EnumerateFileSystemEntries(string rootpath, EnumerationFilterDelegate callback, FileSystemInteraction folderList, FileSystemInteraction fileList) { return EnumerateFileSystemEntries(rootpath, callback, folderList, fileList, null); }
/// <summary> /// Returns a list of all files found in the given folder. /// The search is recursive. /// </summary> /// <param name="rootpath">The folder to look in</param> /// <param name="filter">An optional filter to apply to the filenames</param> /// <param name="callback">The function to call with the filenames</param> /// <returns>A list of the full filenames</returns> public static IEnumerable<string> EnumerateFileSystemEntries(string rootpath, EnumerationFilterDelegate callback) { return EnumerateFileSystemEntries(rootpath, callback, new FileSystemInteraction(System.IO.Directory.GetDirectories), new FileSystemInteraction(System.IO.Directory.GetFiles)); }