c# 關閉StreamWriter 不關閉它的Stream

Is there any way to close a streamwriter without closing its basestreamapp

If you are using .NET Framework 4.5 or later, there is a StreamWriter overload using which you can ask the base stream to be left open when the writer is closed.code

In earlier versions of .NET Framework prior to 4.5, StreamWriter assumes it owns the stream. Options:get

  • Don't dispose the StreamWriter; just flush it.
  • Create a stream wrapper which ignores calls to Close/Dispose but proxies everything else along

This is the overload methodit

public StreamWriter (System.IO.Stream stream, System.Text.Encoding encoding, int bufferSize, bool leaveOpen); io

相關文章
相關標籤/搜索