public static IEnumerable <string> GetFiles(IDiretory root, string searchPattern) { Stack <IDiretory> pendingFolders = new Stack <IDiretory>(); pendingFolders.Push(root); while (pendingFolders.Count != 0) { var path = pendingFolders.Pop(); string[] next = null; try { next = Directory.GetFiles(path.Name, searchPattern); } catch { } if (next != null && next.Length != 0) { foreach (var file in next) { File newFile = new File() { Name = file, Size = new FileInfo(file).Length }; pendingFolders.Push(newFile); yield return(file); } } try { next = Directory.GetDirectories(path.Name); foreach (var subdir in next) { Folder folder = new Folder() { Name = subdir }; pendingFolders.Push(folder); } } catch { } } }
public static IEnumerable<string> GetFiles(IDiretory root, string searchPattern) { Stack<IDiretory> pendingFolders = new Stack<IDiretory>(); pendingFolders.Push(root); while (pendingFolders.Count != 0) { var path = pendingFolders.Pop(); string[] next = null; try { next = Directory.GetFiles(path.Name, searchPattern); } catch { } if (next != null && next.Length != 0) { foreach (var file in next) { File newFile = new File() {Name = file, Size = new FileInfo(file).Length}; pendingFolders.Push(newFile); yield return file; } } try { next = Directory.GetDirectories(path.Name); foreach (var subdir in next) { Folder folder = new Folder() {Name = subdir}; pendingFolders.Push(folder); } } catch { } } }