MemoryStream, Cannot access a closed stream

前端 未结 1 792
失恋的感觉
失恋的感觉 2021-01-11 10:41

With the sharpPDF library I generate a pdf memory stream, and I want to send it directly via email. But the line ms.Seek(.... gives an ObjectDisposedExcepti

相关标签:
1条回答
  • 2021-01-11 11:15

    One simple approach is to get the byte array out of the closed MemoryStream and create another one:

    pdf.CreatePDF(ms)
    ms = new MemoryStream(ms.ToArray())
    
    Dim email As New EmailService
    email.Send(ms)
    

    Note that it's fine to call MemoryStream.ToArray on a closed / disposed instance of MemoryStream. It's even documented:

    Note
    This method works when the MemoryStream is closed.

    0 讨论(0)
提交回复
热议问题