Share via


Recursively listing the files

This function will help you to list all the files with the specified extension recursively i.e. it will also list the files within the child directory.

private

static void BuildList(string searchPath, bool recursive, string ext, ArrayList files)

{

DirectoryInfo parentDir = new DirectoryInfo(searchPath);

DirectoryInfo[] children = parentDir.GetDirectories();

if (recursive && children.Length > 1)

{

foreach (DirectoryInfo Child in children)

{

BuildList(Child.FullName, recursive,ext,files);

}

}

FileInfo[] fileList = parentDir.GetFiles(ext);

foreach (FileInfo file in fileList)

{

files.Add(file.FullName);

}

}