Writing to txt file with StreamWriter and FileStream

前端 未结 2 1666
青春惊慌失措
青春惊慌失措 2021-02-12 08:27

I ran into something interesting when using a StreamWriter with a FileStream to append text to an existing file in .NET 4.5 (haven\'t tried any older f

2条回答
  •  梦谈多话
    2021-02-12 08:48

    It sounds like you did not flush the stream.

    http://msdn.microsoft.com/en-us/library/system.io.stream.flush.aspx

    It looks like StreamWriter writes to a buffer before writing to the final destination, in this case, the file. You may also be able to set the AutoFlush property and not have to explicitly flush it.

    http://msdn.microsoft.com/en-us/library/system.io.streamwriter.autoflush.aspx

    To answer your question, when you use the "using" block, it calls dispose on the StreamWriter, which must in turn call Flush.

提交回复
热议问题