Save text file in fez spider using micro sd module

how to save text file in fez spider using micro sd module ? please send me source code
when i am using persistant storage this error shown .
An unhandled exception of type ‘System.InvalidOperationException’ occurred in GHIElectronics.NETMF.IO.dll. this error shown how to solve.

Biren, you are asking the same question many times. Have you stepped through the guides in the NetMF and Gadgeteer sections of Support – GHI Electronics ? There is a lot of known working code in all those examples, and you will learn a lot by reading them.

Does this help as its part of some code I was going to codeshare as part of Grove Expansion Module. It creates a file ‘AirQuality.txt’ on the MicroSD Card (yes I’m using a MicroSD Module in this project), which contains lines of data separated by a ‘;’ which makes it easy to import into Excel for analysis.



using System;
using System.IO;
using Gadgeteer;
using Gadgeteer.Interfaces;
using Gadgeteer.Modules.GHIElectronics;
using Microsoft.SPOT;
using Gadgeteer.Modules.Seeed;


namespace HydraGrove
{
    public partial class Program
    {
        private static string _dataFilePath = @ "\SD\AirQuality.TXT";
        private StorageDevice _storage;

        private AnalogInput _aq;
        private AnalogInput _mq2;
        private AnalogInput _mq5;

        // This method is run when the mainboard is powered up or reset.   
        private void ProgramStarted()
        {

            Debug.Print("Program Started");

            _mq2 = groveExpansion.SetupAnlogInput(Socket.Pin.Three);

            _aq = groveExpansion.SetupAnlogInput(Socket.Pin.Four);

            _mq5 = groveExpansion.SetupAnlogInput(Socket.Pin.Five);

            sdCard.SDCardMounted += sdCard_SDCardMounted;
            sdCard.SDCardUnmounted += sdCard_SDCardUnmounted;

            if (sdCard.IsCardMounted)
            {
                _storage = sdCard.GetStorageDevice();
            }

            var _timer = new Timer(5000);

            _timer.Tick += _timer_Tick;

            _timer.Start();
        }

        private void sdCard_SDCardUnmounted(SDCard sender)
        {
            _storage = null;
        }

        private void sdCard_SDCardMounted(SDCard sender, StorageDevice SDCard)
        {
            if (sdCard.IsCardMounted)
            {
                _storage = sdCard.GetStorageDevice();
            }
        }

        private void _timer_Tick(Timer timer)
        {
            oledDisplay.SimpleGraphics.Clear();

            double mq2v = _mq2.ReadVoltage();
            double mq2p = _mq2.ReadProportion();

            double mq5v = _mq5.ReadVoltage();
            double mq5p = _mq5.ReadProportion();

            double aqv = _aq.ReadVoltage();
            double aqp = _aq.ReadProportion();

            oledDisplay.SimpleGraphics.DisplayText("MQ2 volts: " + mq2v.ToString(),
                                                   Resources.GetFont(Resources.FontResources.small), Color.Blue, 0, 0);
            oledDisplay.SimpleGraphics.DisplayText("MQ2 pro: " + mq2p.ToString(),
                                                   Resources.GetFont(Resources.FontResources.small), Color.Blue, 0, 14);

            oledDisplay.SimpleGraphics.DisplayText("AQ volts: " + aqv.ToString(),
                                                   Resources.GetFont(Resources.FontResources.small), Color.Red, 0, 31);
            oledDisplay.SimpleGraphics.DisplayText("AQ pro: " + aqp.ToString(),
                                                   Resources.GetFont(Resources.FontResources.small), Color.Red, 0, 45);

            oledDisplay.SimpleGraphics.DisplayText("MQ5 volts: " + mq5v.ToString(),
                                                   Resources.GetFont(Resources.FontResources.small), Color.Yellow, 0, 62);
            oledDisplay.SimpleGraphics.DisplayText("MQ5 pro: " + mq5p.ToString(),
                                                   Resources.GetFont(Resources.FontResources.small), Color.Yellow, 0, 76);

            if (_storage != null)
            {
                try
                {
                    var sw = new StreamWriter(File.Open(_dataFilePath, FileMode.Append, FileAccess.Write));
                    sw.WriteLine(mq2v.ToString() + ";" + mq2p.ToString() + ";" +
                                 aqv.ToString() + ";" + aqp.ToString() + ";" +
                                 mq5v.ToString() + ";" + mq5p.ToString());
                    sw.Flush();
                    sw.Close();
                }
                catch (Exception ex)
                {
                    Debug.Print(ex.Message);
                }
            }
        }
    }
}