-6

Cannot access a closed Stream. Error throwing

Cannot access a closed Stream. Error throwing

How to resolve?

mkl
  • 90,588
  • 15
  • 125
  • 265
  • 2
    What is `createPDF` implementation, please? – Dmitry Bychenko Jul 11 '17 at 10:38
  • html1 = "
    AKHILESH
    "; //byte[] buffer = Encoding.ASCII.GetBytes(html); MemoryStream ms = createPDF(html1); //write to file string path =Server.MapPath("~/Repositery Data/abc.pdf"); FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write); ms.WriteTo(file); file.Close(); ms.Close();
    – user3742747 Jul 11 '17 at 10:38
  • 2
    Could you, please, edit the question: 1. Move the code from link into question's body; 2. Add `createPDF` implementation to the question. – Dmitry Bychenko Jul 11 '17 at 10:42
  • 3
    Read (and *embrace!*): [How do I ask a **good** question?](http://stackoverflow.com/help/how-to-ask) – marc_s Jul 11 '17 at 10:55
  • As you tagged the question with [tag:itext], [this](https://stackoverflow.com/a/25427872/1729265) might solve your issue. If it does, your question would be a duplicate of that question. – mkl Jul 11 '17 at 11:05

1 Answers1

0

This is because something that opens things in memory, e.g streamreader, StreamReader closes the underlying stream automatically when being disposed of. The using statement does this automatically.

However, the StreamWriter is still trying to work on the stream (also, the using statement for the writer is now trying to dispose of the StreamWriter, which is then trying to close the stream).

The best way to fix this is: don't use using and don't dispose of the StreamReader and StreamWriter. See this question.

hiFI
  • 1,887
  • 3
  • 28
  • 57