ksgy2005
ksgy2005

Reputation: 15

C# StreamWriter Buffer Size Does Not Work

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

Answers (1)

Loathing
Loathing

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

Related Questions