Listing files in SD memory generates OutOfMemory condition. G30

I am running a program in a G30. I need to list the files in a SD directory, but when the directory has more than 50 files I got an OutOfMemory exception.

I am using an example program from MSDN site that creates a string[] and loads it with all the filenames from the directory, and most probably that is reason for the OutOfMemory exception, but I don’t know another way to do it.

Any ideas to list the files one by one?

This is the code I am using:

        public static void Dir(string dir)
        {
		string path = "";
		if (dir.Length > 0) path = VolumeInfo.GetVolumes()[0].RootDirectory + @ "\" + dir + @ "\";
		else path = VolumeInfo.GetVolumes()[0].RootDirectory + @ "\";

		string[] dirPaths = Directory.GetDirectories(path);
		int k;
		for (k = 0; k < dirPaths.Length; k++)
		{
		    Debug.Print(dirPaths[k].Substring(4).ToUpper() + @ "\");
		}
		// Here is where I get OutOfMemory condition
		string[] filePaths = Directory.GetFiles(path);
		for (k = 0; k < filePaths.Length; k++)
		{
		    Debug.Print(filePaths[k].Substring(4).ToUpper());
		}
        }

Thanks a lot cyberh0me for your hint.

I’m going to try using Directory.EnumerateFiles

Directory.EnumerateFiles is great !!!

I listed 8,500 files without problem in the G30.

This is the code:

                var files = Directory.EnumerateFiles(path);
                foreach (string file in files)
                {
                    Debug.Print(file.ToUpper());
                }