One of the things driven home during most computer science courses, at least in my era, was all about linked lists, pointers, hexidecimal calculations, bubble sorts, and last but not least, recursive functions.
If you need a slight introduction to recursion, Wikipedia is a good place to start
here. Anyways, I thought I'd bring that same concept when iterating through and creating a list of all files in a directory and all subdirectories. It's a fairly simple concept in which you iterate through each file and then move to the next directory.
Below is a code example of how to create a List<string> including all file names under a certain directory including the subdirectories.
public static List<string> BuildFileList(string targetDirectory)
{
// Create list of files
List<string> files = new List<string>();
// Iterate through all files
string[] fileEntries = Directory.GetFiles(targetDirectory);
foreach(string fileName in fileEntries)
files.Add(fileName);
// Iterate through all directories
string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach(string subdirectory in subdirectoryEntries)
files.AddRange(BuildFileList(subdirectory));
return files;
}
As you can see, it's a pretty straightforward approach to adding things to a list. There are no member variables involved and I'm just adding all my results to my List Generic.
I certainly hope today's CS people still learn the basics like these as they are pretty valuable things!