This code example demonstrates opening and reading from a file found on an SD card connected to a GHI Electronics FEZ board. Just like other storage based operations on a FEZ board – this example contains code that is similar to code that may be used on a PC when accessing a local file system.
This code is written in C# and assumes the development workstation has all required software installed.
using System.IO;
using System.Text;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.IO;
namespace MFConsoleApplication1
{
public class Program
{
static void Main()
{
PersistentStorage sdPS = new PersistentStorage("SD");
sdPS.MountFileSystem();
string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
FileStream FileHandle = new FileStream(rootDirectory + @"hello.txt", FileMode.Open, FileAccess.Read);
byte[] data = new byte[100];
int read_count = FileHandle.Read(data, 0, data.Length);
FileHandle.Close();
Debug.Print("The size of data we read is: " + read_count.ToString());
Debug.Print("Data from file:");
Debug.Print(new string(Encoding.UTF8.GetChars(data), 0, read_count));
sdPS.UnmountFileSystem();
Thread.Sleep(Timeout.Infinite);
}
}
}
For more information on specific namespaces please refer to the following resources: