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.
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
I think you are looking for Flush.
file.Flush();
should do the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With