Share via

StreamWriter - oddity

Darryl Hoar 201 Reputation points
2022-07-27T14:37:37.683+00:00

I have project developed in C# using visual studio 2019.
The project has an export function to export data to a text file in a very specific format.

Application was compiled and installed an operated fine for several month. Recently,
it quit exporting certain data. The database looked fine.

Running the project in visual studio debugger I found that it was executing fine, but that particular data
never appeared in the output text file.

I put a line in StreamWriter.Flush(), and ran the export. All the data was once again exported to the text file.

Super weird. Why did it just start not working.

I have a global variable named exportOut defined for the StreamWriter.

In the code, I have
using (exportOut = new StreamWriter(Filename))
{
the call to the function that generates that type of export data. This function does exportOut.WriteLine(StringOfData);
}

Anybody have any ideas why this would suddenly occur?
thanks.

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


1 answer

Sort by: Most helpful
  1. ShuaiHua Du 636 Reputation points
    2022-07-27T17:55:34.38+00:00

    For performance reason, the content may not be immediately written to your disk when you are writing to a file.

    You txt content is in the buffers, if you invoke the Flush(), the buffers write to disk.

    The StreamWriter has a AutoFlush property, please refer for more :

    StreamWriter.Autoflush

    If right, please Accept.
    Enjoy programming!!!

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.