Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Save Streamwriter after every 200 Cycles without Closing

Tags:

c#

file

save

I'm using a StreamWriter to write some data to a file.

System.IO.StreamWriter file = new System.IO.StreamWriter(path);
while(something_is_happening && my_flag_is_true)
     file.WriteLine(some_text_goes_Inside);

file.close();

What i noticed is, till the close is called no data is written to the file.

Is there any way i can save the contents to the file before closing.

like image 668
user2967920 Avatar asked Dec 05 '22 03:12

user2967920


2 Answers

For this purpose you can use Flush method.

System.IO.StreamWriter file = new System.IO.StreamWriter(path);
int counter = 0;
while(something_is_happening && my_flag_is_true)
{
    file.WriteLine(some_text_goes_Inside);
    counter++;
    if(counter < 200) continue;
    file.Flush();
    counter = 0;
}
file.Close();

For more information welcome to MSDN

like image 171
Alexey Nis Avatar answered Dec 11 '22 11:12

Alexey Nis


I think you are looking for Flush.

file.Flush();

should do the trick.

like image 29
Alex H Avatar answered Dec 11 '22 09:12

Alex H