Simple Directory Recursion
One of the things I liked a lot back when I was a VB programmer was the easy directory recursion using the File System Object, a function I use quite a bit in my applications. It became a bit more complex in Delphi (at least, the way I do it), so since I am now learning C#, I figured it would be wise of me to figure it out yet again. I was surprised about how simple it is
// This will be our ArrayList that everything is stored in.
ArrayList sFileList = new ArrayList();
private void recurseDirs(string sPath)
// First we create an instance of DirectoryInfo and point it to the path we passed as a parameter to the function
DirectoryInfo dirInfo = new DirectoryInfo(sPath);
// Next, we are adding all files in the current path to the ArrayList.
// Now we step through all of the directories in dirInfo, add them to the ArrayList, and then call the function again.
foreach(DirectoryInfo subDirs in dirInfo.GetDirectories())
Using This Code:
As shown in the code, make sure to add System.IO to your namespaces. To call this function from your code, simply type
Obviously, adding everything to an ArrayList is just an example of what to do with the information. I did notice that it will add all temporary, hidden, and system files and folders to the list. There is a workaround for this using FileAttributes, but since I am still a C# beginner, I haven't figured it out yet.