Thursday, May 05, 2005

A simple recursive function to get all files in all sub-dirs of a folder

The below code is in .NET C#

public ArrayList GetAllFiles(string directory)
 ArrayList totalFilesList = new ArrayList(10);
 string[] files = Directory.GetFiles(directory);//add all files in that current  folder.totalFilesList.AddRange(files);//Check if the current directory has sub-directories
 string[] subDirs = Directory.GetDirectories(directory);//if yes, then call recursive functions..
 if(subDirs.Length > 0)
  {//now look for all files in current folder's sub-dir's.
  foreach(string subDir in subDirs)
   ArrayList tempArrayList = GetAllFiles(subDir);
return totalFilesList;