Reputation: 15
I would like to set a small buffer so the buffer can be written to file more frequently. But it seems this does not work. I wrote the following code and check the text file from time to time and find that text is written to file when i = 840 and the file size is exactly 4K, which is the default buffer size. How come?
using (StreamWriter sw = new StreamWriter("u:\\log.txt", true, Encoding.UTF8, 1))
{
for (int i = 0; i < 300000; i++)
{
sw.WriteLine(i);
Console.Write(i);
Console.ReadLine();
}
}
Upvotes: 1
Views: 2071
Reputation: 5256
StreamWriter uses an underlying FileStream, and based on the source code, it looks like the buffer size isn't passed to the file stream.
You can google: .net source code streamwriter
Upvotes: 1