Skip to content

Instantly share code, notes, and snippets.

@LGM-AdrianHum
Created August 17, 2016 04:59
Show Gist options
  • Save LGM-AdrianHum/201b3a8d772eb9cbf118883fd8fd2fd0 to your computer and use it in GitHub Desktop.
Save LGM-AdrianHum/201b3a8d772eb9cbf118883fd8fd2fd0 to your computer and use it in GitHub Desktop.
IterateFiles - Return a IEnumerable of strings.
using System.Collections.Generic;
using System.IO;
namespace SyncFTP
{
public static class OsHelper
{/// <summary>
///
/// </summary>
/// <param name="path"></param>
/// <param name="pattern"></param>
/// <returns></returns>
public static IEnumerable<string> IterateFiles(string path, string pattern)
{
var entryQueue = new Queue<string>();
entryQueue.Enqueue(path);
while (entryQueue.Count > 0)
{
var subdirs = Directory.GetDirectories(entryQueue.Peek());
var files = Directory.GetFiles(entryQueue.Peek(), pattern, SearchOption.TopDirectoryOnly);
foreach (var file in files)
yield return file;
entryQueue.Dequeue();
foreach (var subdir in subdirs)
entryQueue.Enqueue(subdir);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment