Issue regarding the Micro SD Card Module

Hi, I have been stuck by how to write to the Gadgeteer Micro SD Card for two days.

There could be two possible results. The first one is that nothing happened, even the file was not created by the application. The second one is that the file was created with wrong content which means either the number of entries was wrong or the content should have been created in the previous writes appeared in the subsequent write.

I am writing codes on Micro Framework 4.2 using FEZ Spider main board. I also tried with different brands of Micro SD cards (sandisk, pny) with different capacity (4, 8,16G), but all of them failed. I really could use some help.

Here is the code:

    public partial class Program
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");

            temperatureHumidity.MeasurementComplete += temperatureHumidity_MeasurementComplete;

        void temperatureHumidity_MeasurementComplete(GTM.Seeed.TemperatureHumidity sender, double temperature, double relativeHumidity)
            Debug.Print("Temperature: " + temperature +
                ", Relative Humidity: " + relativeHumidity);

            if (!sdCard.IsCardMounted)

            string rootDirectory = sdCard.GetStorageDevice().RootDirectory;
            StreamWriter textFile = new StreamWriter(rootDirectory + @ "\test.txt", true);
            textFile.WriteLine("Hello micro SD card");
            textFile.WriteLine("Hello micro SD card");
            textFile.WriteLine("Hello micro SD card");

            //string msg = "hello kugou";
            //StreamWriter sw = new StreamWriter(File.Open(sdCard.GetStorageDevice().RootDirectory + @ "\test.txt", FileMode.Append, FileAccess.Write));

            //StorageDevice storage = sdCard.GetStorageDevice();
            //string msg = "hello world";
            //byte[] data = Encoding.UTF8.GetBytes(msg + "\n");
            //using (FileStream fs = storage.Open("hello.txt", FileMode.Append, FileAccess.Write))
            //    fs.Write(data, 0, data.Length);
            //    fs.Flush();

Try this for a test. Add this after textFile.Close():


Now remove card and check if a file is there.

EDIT: Also reverse these two lines:

 temperatureHumidity.MeasurementComplete += temperatureHumidity_MeasurementComplete;

@ Hyperlisk - Thank you. It solves the problem.