Main Site Documentation

[Closed temporarly] StreamWriter write, starting with a new line


Hi everyone, there’s something weird in my application that I can understand. maybe someone will help me in figuring out what may be wrong…

I am data logging some data into a csv file. The file is supposed to have a fixed number of field.

The method i am using is:

public bool SDC_write_line(string file_name, string line)
            FileInfo fi1 = new FileInfo("\\SD\\" + file_name);
            if (fi1.Exists)
                    using (StreamWriter SW = new StreamWriter("\\SD\\" + file_name, true))

                        SW.Write("\n" + line);
                catch (Exception E)
                    DebugInfo.Print("Exception" + E.ToString() + "while writing " + line + " in " + file_name);
                return true;
            return false;

To be sure that a new line is created at each write, I am using “\n”. However I observe line that exceed the number of fields like if the /n was not there… how can it be possible?


If you use StreamWriter you can use WriteLine() then you do not have to care about the line ending symbols. Every time you call the function it pushes a single line to the file.


I can try it.

What I notice also is that the /n seems not to be taken into account each time a previous write has failed (due to a power loss I think). This seems that the \n and a part of the line is eaten by the last part of the previous line


For this issue you have to call Flush() of the VolumeInfo class.


I am calling this method each time after writeline:

public static void SDC_flushfast()
            catch (Exception e)
                DebugInfo.Print("FLUSH SDC Not OK " + e.Message);

But what about if a powerloss occurs before calling SDC_flushfast()? Sould I recall flush before writing?


Well that case you can’t handle in software. But if you call Flush() after each line, you can ensure that your loss is maximum 1 line.


I will clean the logs in order to check if it happens again. I have a doubt about the cause. The logs file have been retrieve back through a udp datagram. Maybe packet have been lost during file tranfert and what the customer gave to me does not reflect what there’s on the SD card…


You should read the documents page on flush() too…

Big warning that says:

[quote]Under current versions of .NET Micro Framework, the Flush method of FileStream will eventually actually write the buffers; unfortunately, there is a variable time delay, which reportedly can be as large as a minute (see: ). To guarantee that the file buffers and the meta-data are written to the media you need to flush the volume: VolumeInfo.GetVolumes([0].FlushAll() or new Microsoft.SPOT.IO.VolumeInfo("\SD").FlushAll(). For more information see the warning in the NETMF file system documentation.



As you can see in the code above, I am using flushall in the method. As I said before, I am not sure if it’s a problem of writing or if its a packet loss that has occurs when I asked the customers to retrieve logs back using an ethernet udp transfert… I need to check on the SD card…