Can you keep a StreamReader from disposing the underlying stream?

前端 未结 9 2053
小鲜肉
小鲜肉 2020-11-30 09:22

Is there a way to do this:

this.logFile = File.Open(\"what_r_u_doing.log\", FileMode.OpenOrCreate, FileAccess.ReadWrite);

using(var sr = new StreamReader(th         


        
9条回答
  •  离开以前
    2020-11-30 10:17

    .NET 4.5 will finally fix this problem with a new constructors on StreamReader and StreamWriter that take a leaveOpen parameter:

    StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen)
    
    StreamWriter(Stream stream, System.Text.Encoding encoding, int bufferSize, bool leaveOpen)
    

提交回复
热议问题