Search code examples
c#filestreamstreamreaderstreamwriter

Why StreamWriter can't access the closed stream but StreamReader can?


If I write following statements.

fileStream.Close();
reader.Close();
writer.Close();

reader.Close() statement executes successfully. But I get error "Can't open closed file." on 3rd statement writer.Close()

If I write

fileStream.Close();
writer.Close();
reader.Close();

2nd statement i.e. writer.Close() itself throws the same exception.

Does anyone have idea?


Solution

  • I think it's because writer calls flush() before closing the underlying stream.