README.md
December 5, 2022 ยท View on GitHub
Description
This shows and explains how to recursivley add all files, folders, and subfolder to an ArrayList.
More Info
| Submitted On | |
| By | bleh |
| Level | Beginner |
| User Rating | 4.8 (19 globes from 4 users) |
| Compatibility | C# |
| Category | Files |
| World | .Net (C#, VB.net) |
| Archive File |
Source Code
Simple Directory Recursion
Introduction:
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
in C#.
using System.IO;
...
// 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.
sFileList.AddRange(dirInfo.GetFiles());
// 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())
{
sFileList.Add(subDirs.FullName);
recurseDirs(subDirs.FullName);
}
}
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
recurseDirs("C:\\Whatever\\Path\\Here\\");
In Conculsion:
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.