Main Site Documentation

[Answered]SD Card code error?


#1

On my Panda II with current SDK’s and VS2010 I get the following error when trying to compile:

[italic]Error 1 The name ‘Directory’ does not exist in the current context[/italic]

It occurs on lines:

string[] files = Directory.GetFiles(e.Volume.RootDirectory);
                string[] folders = Directory.GetDirectories(e.Volume.RootDirectory);

I have no doubt it is another noob error but I have spent the better part of a day trying to find the issue. Could someone put me back on the right path? (BTW: This code was copied from wiki.tinyclr.com)

using System;
using System.IO;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.System; //must be used to or compile error occurs at "sdcard = new persistentstorage("sd");"
using GHIElectronics.NETMF.USBHost;


namespace Test
{
    class Program
    {
        // Hold a static reference in case the GC kicks in and disposes it automatically, note that we only support one in this example!
        static PersistentStorage SDCard;

        public static void Main()
        {
            // Subscribe to RemovableMedia events
            RemovableMedia.Insert += RemovableMedia_Insert;
            RemovableMedia.Eject += RemovableMedia_Eject;

            // Assume SD card is inserted
            // Create a new storage device
            SDCard = new PersistentStorage("SD");
            SDCard.MountFileSystem();

            // Sleep forever
            Thread.Sleep(Timeout.Infinite);
        }

        static void RemovableMedia_Insert(object sender, MediaEventArgs e)
        {
            Debug.Print("Storage \"" + e.Volume.RootDirectory + "\" is inserted.");
            Debug.Print("Getting files and folders:");
            if (e.Volume.IsFormatted)
            {
                string[] files = Directory.GetFiles(e.Volume.RootDirectory);
                string[] folders = Directory.GetDirectories(e.Volume.RootDirectory);
                
                Debug.Print("Files available on " + e.Volume.RootDirectory + ":");
                for (int i = 0; i < files.Length; i++)
                    Debug.Print(files[i]);

                Debug.Print("Folders available on " + e.Volume.RootDirectory + ":");
                for (int i = 0; i < folders.Length; i++)
                    Debug.Print(folders[i]);
            }
            else
            {
                Debug.Print("Storage is not formatted. Format on PC with FAT32/FAT16 first.");
            }

            // We do not need it any more
            SDCard.Dispose();
        }

        static void RemovableMedia_Eject(object sender, MediaEventArgs e)
        {
            Debug.Print("Storage \"" + e.Volume.RootDirectory + "\" is ejected.");
        }
    }
}

#2

Make sure you have added System.IO.dll to your referenced assemblies.


#3

Thanks for your help. I guess I am going to be taking a class in C#(VB background does not help)!!!


#4

Beginner Guide e-book is a good start:

http://wiki.tinyclr.com/index.php?title=Category:Beginner_Guide_ebook