How do I copy the contents of one stream to another?

后端 未结 13 2364
悲哀的现实
悲哀的现实 2020-11-21 22:11

What is the best way to copy the contents of one stream to another? Is there a standard utility method for this?

13条回答
  •  误落风尘
    2020-11-21 22:41

    The following code to solve the issue copy the Stream to MemoryStream using CopyTo

    Stream stream = new MemoryStream();
    

    //any function require input the stream. In mycase to save the PDF file as stream document.Save(stream);

    MemoryStream newMs = (MemoryStream)stream;
    
    byte[] getByte = newMs.ToArray();
    

    //Note - please dispose the stream in the finally block instead of inside using block as it will throw an error 'Access denied as the stream is closed'

提交回复
热议问题