I think that in terms of code readability, it's best to write a recursive function. A recursive function is one that calls itself until it reaches a point where it does not need to call any other function.
To illustrate, the factorial n is written as n! and defined as 1 x 2 x 3 x ... xn (where n is a positive integer), can be easily determined recursively as follows.
public int factorial(int n) { if (n < 0) { throw new Exception("A factorial cannot be calculated for negative integers."); } if (n == 0 || n == 1) { // end condition, where we do not need to make a recursive call anymore return 1; } else { // recursive call return n * factorial(n - 1); } }
NB: 0! and 1! defined as 1.
Similarly, the method of listing all files and folders by a given path can also be defined recursively. This is because files and folders have a recursive structure.
Therefore, a method like the following will work:
public static List<FileSystemInfo> GetAllFilesAndFolders(string folder) { // NOTE : We are performing some basic sanity checking // on the method formal parameters here if (string.IsNullOrEmpty(folder)) { throw new ArgumentException("An empty string is not a valid path.", "folder"); } if (!Directory.Exists(folder)) { throw new ArgumentException("The string must be an existing path.", "folder"); } List<FileSystemInfo> fileSystemInfos = new List<FileSystemInfo>(); try { foreach (string filePath in Directory.GetFiles(folder, "*.*")) { // NOTE : We will add a FileSystemInfo object for each file found fileSystemInfos.Add(new FileInfo(filePath)); } } catch { // NOTE : We are swallowing all exceptions here // Ideally they should be surfaced, and at least logged somewhere // Most of these will be security/permissions related, ie, // the Directory.GetFiles method will throw an exception if it // does not have security privileges to enumerate files in a folder. } try { foreach (string folderPath in Directory.GetDirectories(folder, "*")) { // NOTE : We will add a FileSystemInfo object for each directory found fileSystemInfos.Add(new DirectoryInfo(folderPath)); // NOTE : We will also add all FileSystemInfo objects found under // each directory we find fileSystemInfos.AddRange(GetAllFilesAndFolders(folderPath)); } } catch { // NOTE : We are swallowing all exceptions here // Ideally they should be surfaced, and at least logged somewhere // Most of these will be security/permissions related, ie, // the Directory.GetDirectories method will throw an exception if it // does not have security privileges to enumerate files in a folder. } return fileSystemInfos; }
It should be noted that this method will "walk" through the entire directory structure under the folder and will NOT return until it "destroys" all the heredity. Thus, it may take a long time to return if there are many objects that can be found.
Another thing to note is that the readability of this method can be further improved with the help of Lambda expressions and extension methods.
Note. The problem with using Directory.GetFiles and Directory.GetDirectories for recursing subfolders is that if there are any exceptions (for example, related to security permissions), the method will return nothing, while recursing manually allows you to handle these exceptions and still get a set of files back.