SD card help

How do i append a line of text to a file every 5 seconds based on time.

This is what i got so far, its crashing

 #### Exception System.IO.IOException - CLR_E_UNAUTHORIZED_ACCESS (1) ####
    #### Message: 
    #### System.IO.FileSystemManager::AddToOpenList [IP: 0086] ####
    #### System.IO.FileStream::.ctor [IP: 005b] ####

  while (true)
                sdcard.SaveDataToFile("Test", DateTime.Now.DayOfYear.ToString());

 public void CreateDirectory(string name) {

        public void SaveDataToFile(string data, string fileName)
            FileStream file = new FileStream($"{Directory.GetCurrentDirectory()}{fileName}.txt", FileMode.OpenOrCreate);
            file.Seek(0, SeekOrigin.End);
            byte[] bytes = Encoding.UTF8.GetBytes($"{DateTime.Now}-{data}{Environment.NewLine}");
            file.Write(bytes, 0, bytes.Length);

I want to create a folder each year, and a file each dayOfYear and data every 5 seconds to that file

The docs are as usual incomplete, and .NET apis are completely different.

It is crashing because you never close the file. You should close after writing.

file.write(bytes, 0, bytes.Length);

You could probably make it easier by opening the file in append mode.

FileStream file = new FileStream($"{Directory.GetCurrentDirectory()}{fileName}.txt", FileMode.Append);

The docs are as usual incomplete, and .NET apis are completely different.

Disagree. Me, and I think many others, managed to get things working with the GHI docs and the .NET API documentation. Why would GHI make their docs a replica of the .NET docs? No point in that…

1 Like

this is actually relevant to an issue I’m currently dealing with as well. Once it’s working, let it run for a while and see if it eventually will fail to write to the card.

Only Close is what was needed. Flush after Close gives an error.
Close is not mentioned in the docs…

@sgtyar95 what do you mean by fail?
does it crash after a while? how long?
or the data is just missing? do you remove sd card?

i let it run for 2 hours every 5 seconds it run OK, i then just remove the card, and last ~30 seconds is lost due to removal

File flush doesn’t save everything. There is a media flush… I am trying to remember…

You’re right. You should flush before closing.
I typed that code from memory…

does it crash after a while? how long?

In my experience it only goes wrong if the power is pulled out at the wrong moment, this can corrupt the SD card requiring a format.

Usually it’ll run for ten minutes or so (writes every ten seconds) then it will throw an InvalidOperationException when i try to write to the card. It will open and read OK, but it will no longer write properly until I reformat it.

it seems to be related to which card I’m using though, I currently have a dev board on my desk that’s been running for a day now with no issues with a different card. I am just interested to see if anyone else has seen this behavior.

What kind of good cards and bad cards?

I bought 10 of the cheapest i could find locally, they’re running fine :stuck_out_tongue:

I also seem to have a problem with the SD card. I have a SQLite database with approximately 1500 rows with 11 integer columns. Sometimes I get the following exception:

#### Exception GHIElectronics.TinyCLR.Data.SQLite.QueryExecutionException - 0x00000000 (38) ###
#### Message: disk I/O error
#### GHIElectronics.TinyCLR.Data.SQLite.SQLiteDatabase::ExecuteNonQuery [IP: 002d] ####

In NETMF with the speed parameter “SDCard.Mount (2500)” everything worked correctly.

What parameters should have an SD card for seamless operation in SITCore devices?

There is no option to set SD lock on TinyCLR.

It is on the list : Support SD Clock · Issue #844 · ghi-electronics/TinyCLR-Libraries (

dual wielding sd card

Can you provide a small code that we can reproduce? Following what we see, it could be depend on SD card quality but hopefully we can see something in our cards.

TinyCLR has clock fixed 24 or 25Mb I believe. In netmf if you increase from 2.5 to 10 or 12Mb, if it failed then that card doesn’t happy with higher speed.