Correct way to Initialize/Open SD Card - Fez Cerberus

I have the method below which is to open and close the SD Card, it’s called from a loop, on first method call I get the unhandled exception shown attached and the output window shows the message below, on all subsequent calls it works without any errors?? Why would this be, I’ve no idea.

OUTPUT WINDOW MESSAGE
–A first chance exception of type ‘System.IO.IOException’ occurred in Microsoft.SPOT.IO.dll
An unhandled exception of type ‘System.IO.IOException’ occurred in Microsoft.SPOT.IO.dll–

for (byte i = 0; i < 100; i++)
{
     Debug.Print(i.ToString());
     TestSDCard();
     Thread.Sleep(100);
}

public static void TestSDCard()
{
      var SdCard = new SDCard();
      SdCard.Mount();
      Thread.Sleep(100);
      SdCard.Unmount();
      SdCard.Dispose();
}

On which statement is the exception occurring?

Have you read the documentation on what is recommended after a mount?

Are you waiting long enough after the mount before unmounting?

You are posing under the Gadgeteer forum but using NETMF API. Are you using the wrong API or have you posted in wrong forum?

@ Mike - Thanks, can you direct me to the documentation you mention with regards to what should be done after a mount, the unhandled exception occurs after the method returns.

I received this same error while refactoring my project from 4.3.3.0 to 4.3.6.0 in VS 2013. I commented out all the SDCard stuff (class initialization, Mount(), Unmount()) and I still seem able to access the SD card.


 if (Microsoft.SPOT.IO.VolumeInfo.GetVolumes()[0].IsFormatted)
 {
                    string rtDirectory = Microsoft.SPOT.IO.VolumeInfo.GetVolumes()[0].RootDirectory;
                    System.IO.Directory.SetCurrentDirectory(rtDirectory);

                    string[] files = System.IO.Directory.GetFiles(rtDirectory);
                    string[] folders = System.IO.Directory.GetDirectories(rtDirectory);

                    Debug.Print("Files available on " + rtDirectory + ":");
                    for (int i = 0; i < files.Length; i++)
                        Debug.Print(files[i]);

                    Debug.Print("Folders available on " + rtDirectory + ":");
                    for (int i = 0; i < folders.Length; i++)
                        Debug.Print(folders[i]);
}

I might be wrong, maybe I am using the wrong API configuration, or it might be specific to my hardware. FEZ Cobra II.

@ DanW - Thanks, yes it seems to work nicely if I mount the SdCard only in the Init method, and comment out all other SdCard lines of code, for my application the SD Card will always be the same card, and will always be inserted

Look in the GHI NETMF documentation on the support page. Find the SDCard class.