This code example demonstrates how to mount a mass storage USB device from a GHI Electronics FEZ board. Take note that some of the code contained herein is similar to code used on a PC while working with the local file system. Therefore, some of this code may be familiar if you have worked with the System.IO namespace in .NET.
This code is written in C# and assumes the development workstation has all required software installed.
using System;
using System.IO;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.USBHost;
namespace Test
{
class Program
{
static PersistentStorage ps;
public static void Main()
{
RemovableMedia.Insert += RemovableMedia_Insert;
RemovableMedia.Eject += RemovableMedia_Eject;
USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;
Thread.Sleep(Timeout.Infinite);
}
static void DeviceConnectedEvent(USBH_Device device)
{
if (device.TYPE == USBH_DeviceType.MassStorage)
{
Debug.Print("USB Mass Storage detected...");
ps = new PersistentStorage(device);
ps.MountFileSystem();
}
}
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.");
}
}
static void RemovableMedia_Eject(object sender, MediaEventArgs e)
{
Debug.Print("Storage "" + e.Volume.RootDirectory + "" is ejected.");
}
}
}
For more information on specific namespaces please refer to the following resources: