I'm working on a Netduino Plus application that's reading/writing files to/from an SD card. I'm looking for a memory efficient way to enumerate through the list of files on the SD card. For the sake of this discussion, I don't know the names of the files on the card, so my code needs to enumerate through a list. It seems like the GetFiles() method of a DirectoryInfo uses memory in some direct proportion to the number of files (and perhaps the length of the filenames or even file size?) on the disc. As such, there's a limit to the number of files GetFiles can handle. Based on my filenames/file sizes, I'm capping out around 100 files.
Is there another way to enumerate files on the SD card? Something that does not involve GetFiles()?
My code looks similar to this:
ArrayList fileInfo = new ArrayList();
if (Directory.Exists(path))
{
DirectoryInfo dirInfo = new DirectoryInfo(path);
foreach (FileInfo file in dirInfo.GetFiles())
{
fileInfo.Add(file);
}
}
The code poops out on the foreach statement (OutOfMemory exception).