How to recursively list all the files in a directory and child directories in C#?
A simple and clean solution
///
/// Scans a folder and all of its subfolders recursively, and updates the List of files
///
/// Full path of the folder
/// The list, where the output is expected
internal static void EnumerateFiles(string sFullPath, List fileInfoList)
{
try
{
DirectoryInfo di = new DirectoryInfo(sFullPath);
FileInfo[] files = di.GetFiles();
foreach (FileInfo file in files)
fileInfoList.Add(file);
//Scan recursively
DirectoryInfo[] dirs = di.GetDirectories();
if (dirs == null || dirs.Length < 1)
return;
foreach (DirectoryInfo dir in dirs)
EnumerateFiles(dir.FullName, fileInfoList);
}
catch (Exception ex)
{
Logger.Write("Exception in Helper.EnumerateFiles", ex);
}
}