This is a simple tool for traversing the file system in search for a file or folder or doing something to many files.

The source is available here: http://www.bodurov.com/files/FileSystemIterator.zip

And here is a sample application that searches for the first file with extension .aspx

    public class Program
    {
        private FileInfo _file;

        static void Main(string[] args)
        {
            Program p = new Program();

            FileSystemIterator fsi = new FileSystemIterator(@"C:\inetpub\wwwroot");
            fsi.ProcessElement += p.FindFile;
            fsi.Run();

            Console.WriteLine((p.File == null) ? "No Files Found" : p.File.Name);
        }

       FileInfo File { get { return this._file; } }

        void FindFile(object sender, FileSystemElementArgument e)
        {
            if (e.IsFile)
            {
                Console.WriteLine("Current File: {0} Current Level: {1}", e.Name, e.Level);

                if(e.Name.EndsWith(".ascx", StringComparison.InvariantCultureIgnoreCase))
                {
                    this._file = e.FileInfo;
                    // ends traversing the file system tree
                    e.Cancel = true;
                }
            }
        }

    }

Share this post:   digg     Stumble Upon     del.icio.us     E-mail

Commenting temporarily disabled