El otro dia estaba viendo procedimientos que necesitan ejecutar en un segundo hilo para no bloquear la aplicacion, e implemente algo muy sencillo para buscar archivos de manera "iterativa":
using System;
using System.Collection.Generic;
using System.IO;
public class FileSeeker
{
public static string[] RetrieveFilesFrom(string directory, string searchPattern,int level)
{
Listfilenames = new List ();
Stackdirectories = new Stack ()
directories.Push(directory);
while(directories.count!=0)
{
string currentDirectory = directoriesStack.Pop();
foreach(string file in Directory.GetFiles(currentDirectory,searchPattern))
filenames.Add(file);
if((level==-1) || (directories.count != level))
{
foreach(string subDirectory in Directory.GetDirectories(currentDirectory))
directories.Push(subDirectory);
}
}
return filenames.ToArray();
}
}
public class Program
{
public static void Main(string[] args)
{
foreach(string path in FileSeeker.RetrieveFilensFrom(@"C:\Program Files\", "*.exe",-1)
Console.WriteLine(path);
Console.Read();
}
}
Now I'm encapsulating all the behavior in a component with more features, like search using regular expressions, file size, creation time, etc. As soon I finish it, I will write an article for elguille's site.