Want to simply append to a text file on an SD Module

Problem:

No matter what code i try I can’t successfully “append” text to a mounted SD card… i tried the below code but it doesn’t work.

The name of the product & Details on your setup:

      Spider mainboard w/ SD Module

The code you’re trying to run:

string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
FileStream FileHandle = new FileStream(rootDirectory + @ “\log.txt”, FileMode.OpenOrCreate);
byte[] data = Encoding.UTF8.GetBytes(sInput);
FileHandle.Write(data, 0, data.Length);
FileHandle.Close();

Your question is missing one or more of these details:[ul]
The name of the product.
The code you’re trying to run.
Details on your setup.[/ul]
(Generated by QuickReply)

Would this help?



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

                    if (_storage != null)
                    {
                        string path = _storage.RootDirectory + @ "\Data.txt";
                        StreamWriter sw = new StreamWriter(path, true);
                        sw.WriteLine("whatever your trying to write");
                        sw.Flush();
                        sw.Close();
                    }
                }
                catch (Exception x)
                {
                    Debug.Print(x.Message);
                }
            }

Nope… still didn’t work… sdCard.GetStorageDevice() works and all looks good to StreamWriter sw = new StreamWriter(path, true);… it just won’t write to the file… using MF 4.2, latest firmware for spider, tried using a non-kingston SD, tried using a different SD module, still nothing.

###################### from Progam as a quick test am calling like this:

Utility.Initialize(sdMod, @ “\Log.txt”);
Utility.LogToSD(“Test 3”, true);
Utility.LogToSD(“Test 4”, true);

####################### to utility.cs

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.IO;
using System.Text; // needed for Decoder

using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

using Microsoft.SPOT.IO;

namespace TestProgram
{
    public static class Utility
    {
        private static SDCard _sdMod;
        private static string _sLogFilePath;

        public static void Initialize(SDCard sdMod, string sLogFilePath)
        {
            _sdMod = sdMod;
            _sLogFilePath = sLogFilePath;
        }

        public static void LogToSD(string sInput)
        {
            Utility.LogToSD(sInput, true);
        }

        public static void LogToSD(string sInput, bool bPrintFlag)
        {
            try
            {
                if (bPrintFlag)
                {
                    Debug.Print(sInput);
                }

                if (_sdMod.IsCardMounted)
                {
                    GT.StorageDevice _storage = _sdMod.GetStorageDevice();

                    if (_storage != null)
                    {
                        string path = _storage.RootDirectory + _sLogFilePath;
                        StreamWriter sw = new StreamWriter(path, true);
                        sw.WriteLine(sInput);
                        sw.Flush();
                        sw.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
        }
    }
}

@ carpenoctem - What is the File System of SD card? Could you see contents of card, eg directories, folders?

I dont see this code anywhere in your code:

PersistentStorage sdPS;
sdPS = new PersistentStorage("SD");
sdPS.MountFileSystem();

Sorry, I program with bare netmf. I have to assume that the lines I mentiond are already taken care of by the Gadgeteer module drivers…

@ JohnySeven - plain ole FAT

@ GMod(Errol) - I don’t have the code you posted in my code… I didn’t need to have it to read from a file on the SD… do I need to have it to write???

@ carpenoctem - Yes you need until the SD MMC mapping will not de done.

Just try first this :

Mount,
Format the SD
Ask if IsFormatted
Unmount

Format will somteimes tkae a couple of seconds, but if done correctly, it will return true to the IsFormatted property.