So you want to start at a top level folder, and then process all the folders beneath… Maybe you really do want to look at every file (maybe count the total size of the folder), maybe you want to process all the XML files there. The most obvious route is to recursively search through each folder;
static void Main(string[] args){string startFolder = @"C:\temp";List<string> contents = new List<string>();foreach (string dir in Directory.GetDirectories(startFolder))ProcessFolder(dir, contents);foreach (string fileName in contents)Console.WriteLine(fileName);Console.ReadKey();}static void ProcessFolder(string folder, IList<string> theList){foreach (string file in Directory.GetFiles(folder))theList.Add(folder + "\\" + file);
foreach (string dir in Directory.GetDirectories(folder))ProcessFolder(dir, theList);}
All well and good but, at some point (probably due to the depth of the file system) you will run out of stack space. A better way to traverse the folder structure is to do an iterative search;
private void ProcessFolder(string startingPath){int iterator = 0;
List<string> dirList = new List<string>();dirList.Add(startingPath);string parentFolder = startingPath;
// Every new folder found is added to the list to be searched. Continue until we have
// found, and reported on, every folder or the calling thread wants us to stop
while (iterator < dirList.Count && !(workerThreadInfo.StopRequested))
{parentFolder = dirList[iterator]; // Each FileTreeEntry wants to know who its parent is
try
{foreach (string dir in Directory.GetDirectories(dirList[iterator])){AddFolder(parentFolder, dir, dir);dirList.Add(dir);}foreach (string filename in Directory.GetFiles(dirList[iterator])){FileInfo file = new FileInfo(filename);
AddFile(parentFolder, file.Name, file.Length);}}// There are two *acceptable* exceptions that we may see, but should not consider fatal
catch (UnauthorizedAccessException)
{}catch (PathTooLongException)
{}iterator++;}}
Now, we iterate through each discovered folder and for each discovered file, we call an external routine (in this case “AddFile()”. Note the two caught exceptions which can occur but which, in the author’s opinion, are not important in this context;
- UnauthorizedAccessException
- OK; ya got me. I’m not allowed in here, so let’s continue and not break the calling app
- PathTooLongException
- This is a funny one. Win200x sets a maximum path length of 255 characters. Create a big and complex structure (especially a Java one), zip it and then unravel it under a folder that is maybe 100 characters long. Windows is happy to unzip this, and even display it in the folder view. But try and open the file and you’ll be stuffed
So, ignoring these, this routine will handle any files within a structure, irrespective of how deep that structure gets.