Sd card problem

Hi, i want to write a list into my SDcard, but i don’t know why, it doesn’t work. Help plz.
My code :


            PersistentStorage SD = new PersistentStorage("SD");
            SD.MountFileSystem();
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            FileStream FileHandle = new FileStream(rootDirectory + @ "\X.txt", FileMode.Create);
            
            FileStream FileHandle2 = new FileStream(rootDirectory + @ "\Y.txt", FileMode.Create);
            

for (int i = 0; i < number; i++)
            {
                byte[] data = Encoding.UTF8.GetBytes(ListeX[i].ToString() + " ");
                FileHandle.Write(data, 0, data.Length);
                data = Encoding.UTF8.GetBytes("c'est fait");
                FileHandle.Write(data, 0, data.Length);
            }
            FileHandle.Close();
            
            
            for (int i = 0; i < number; i++)
            {
                byte[] data = Encoding.UTF8.GetBytes(ListeY[i].ToString() + " ");
                FileHandle2.Write(data, 0, data.Length);
            }


Not much information to work with… What happens? Do you get an exception? What mainboard? Gadgeteer or plain MF?

Assuming that the SD card is inserted in the holder, try adding a Thread.Sleep() after the mounting of the file system.

i have a Fez panda 2,
when i execute my code, finally i have 2 file : X.txt and Y.txt on the Sd card, but they are empty

more details:


public static void SDsave(PersistentStorage SD,FileStream FileHandle,FileStream FileHandle2, string rootDirectory)
        {


            for (int i = 0; i < number; i++)
            {
                byte[] data = Encoding.UTF8.GetBytes(ListeX[i].ToString() + " ");
                FileHandle.Write(data, 0, data.Length);
                data = Encoding.UTF8.GetBytes("c'est fait");
                FileHandle.Write(data, 0, data.Length);
            }
            FileHandle.Close();
            
            
            for (int i = 0; i < number; i++)
            {
                byte[] data = Encoding.UTF8.GetBytes(ListeY[i].ToString() + " ");
                FileHandle2.Write(data, 0, data.Length);
            }}



public static int number = 0;


public static int RAM(int number, PersistentStorage SDcard, bool force, FileStream FileHandle, FileStream FileHandle2, String rootDirectory)
        {

            if (number > 400 || force == true)
            {
                SDsave(SDcard, FileHandle,FileHandle2, rootDirectory );
                number = 0;
            }
            
        }

public static void Main()                                                             
        {
            PersistentStorage SD = new PersistentStorage("SD");
            SD.MountFileSystem();
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            FileStream FileHandle = new FileStream(rootDirectory + @ "\X.txt", FileMode.Create);
            
            FileStream FileHandle2 = new FileStream(rootDirectory + @ "\Y.txt", FileMode.Create);

...
RAM(number, SD, true, FileHandle, FileHandle2, rootDirectory);
....

}









i deleted

public static number=0; 

and added just int number = 0 in my main()
but it doesn’t work again.
Than i deleted the loop

for (int i = 0; i < number; i++)

and just wrote :

 byte[] data = Encoding.UTF8.GetBytes("hello");
                FileHandle.Write(data, 0, data.Length);  

public static void SDsave
but it didn’t work.
.
But when i put, just

 byte[] data = Encoding.UTF8.GetBytes("hello");
                FileHandle.Write(data, 0, data.Length);  

in Main() , it works, also i think that i am going to delete my RAM, and SDsave methods.

  • There are another problem :
    when i write into Main() :
byte[] data = Encoding.UTF8.GetBytes("hello");
                FileHandle.Write(data, 0, data.Length);
                FileHandle2.Write(data, 0, data.Length);

Than i haven’t anything in the Y.txt file, and i have “hello” in the X.txt file.
How can i save “hello”, into both the Y and X files.
thx

You never closed FileHandle2 in the original code, so no data will appear.

can i FileHandle.Close(), than open it again ? and how to do that plz ?

Once you close a file, you need to instantiate another object to gain access.

If you want to have data written to disk, without closing the file object, then use the Flush() method.

If you want to read and write text to a file, you might want to consider the StreamReader and the StreamWriter classes.

ok,
i put a FileHandle.Close(), on the end of my code, and it works
thx :slight_smile: