Get Files Recursive - CSharp File IO

CSharp examples for File IO:Directory

Description

Get Files Recursive

Demo Code


using System.Text;
using System.Linq;
using System.IO;/* w  w  w .java 2s  .co  m*/
using System.Collections.Generic;
using System;

public class Main{
        public static IEnumerable<FileInfo> GetFilesRecursive(this DirectoryInfo dirInfo, string searchPattern)
        {
            // ignore hidden directories (these are usually owned by version control software)
            if ((dirInfo.Attributes & FileAttributes.Hidden) == 0)
            {
                foreach (DirectoryInfo di in dirInfo.GetDirectories())
                {
                    foreach (FileInfo fi in GetFilesRecursive(di, searchPattern))
                    {
                        yield return fi;
                    }
                }

                foreach (FileInfo fi in dirInfo.GetFiles(searchPattern))
                {
                    yield return fi;
                }
            }
        }
        public static IEnumerable<FileInfo> GetFilesRecursive(this DirectoryInfo dirInfo)
        {
            return GetFilesRecursive(dirInfo, "*.*");
        }
}

Related Tutorials