This code example demonstrates how to open and write to a file found on an SD card connected to a GHI Electronics FEZ board. Just like other SD/USB operations for FEZ – you can expect to find code similar to that of working with the Local File System of a PC (i.e. System.IO namespace).
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.Create);
byte[] data = Encoding.UTF8.GetBytes("This string will go in the file!");
FileHandle.Write(data, 0, data.Length);
FileHandle.Close();
sdPS.UnmountFileSystem();
Thread.Sleep(Timeout.Infinite);
}
}
}
For more information on specific namespaces please refer to the following resources: