Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Memory efficient way to enumerate an array of FileInfo


  • Please log in to reply
2 replies to this topic

#1 jdsmith

jdsmith

    Member

  • Members
  • PipPip
  • 26 posts

Posted 23 August 2011 - 05:39 PM

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).

#2 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 23 August 2011 - 06:32 PM

Is there another way to enumerate files on the SD card?

I have not tried it yet, but in .NET MF 4.2 (you'd need to update to 4.2.0 RC1 firmware), there are new Directory.EnumerateFiles(...) and Directory.EnumerateDirectories(...) static methods that should work even for large amount of entries. For more detailed information, please have a look at CodePlex issue 671 (resolved with changeset 12496).

#3 jdsmith

jdsmith

    Member

  • Members
  • PipPip
  • 26 posts

Posted 23 August 2011 - 06:58 PM

I have not tried it yet, but in .NET MF 4.2 (you'd need to update to 4.2.0 RC1 firmware), there are new Directory.EnumerateFiles(...) and Directory.EnumerateDirectories(...) static methods that should work even for large amount of entries. For more detailed information, please have a look at CodePlex issue 671 (resolved with changeset 12496).


Thanks! I'll give it a try.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.