title: Cannot access a closed stream when passing stream to another function
body: <p>I need to pass a stream to a couple of functions in another class, but its throwing an error</p>
  
  <blockquote>
    <p>Cannot access a closed stream</p>
  </blockquote>
  
  <p>Here's the code:</p>
  
  <p>first method: </p>
  
  <p>Here it opens a file with File.Open method and then creates a memorystream object and it copies FileStream to MemoryStream. then sets Position to 0 (i set position to 0, because i was that in a solution, but not helping tho). Then it creates an object of class <code>DocxConvert</code> and call the <code>Converto</code> method by passing MemoryStream to it.</p>
  
  <pre><code>    using (var stream = File.Open(tempPath2, FileMode.Open))
      {
          using(var ms = new MemoryStream())
              {
                  stream.CopyTo(ms);
                  ms.Position = 0;
                  using (var docx = new DocxConvert())
                  {
                      return docx.Converto(ms);
                  }
              }
      }
  </code></pre>
  
  <p>DocxConvert Class: </p>
  
  <p>It takes stream and then calls <code>copyStream</code> method by passing the accepted stream.</p>
  
  <p>copyStream method in DocxConvert Class: it should copy the accepted stream to another stream called <code>_memoryStream</code> which is a class property.</p>
  
  <pre><code>    public class DocxConvert
  {  
  
          private MemoryStream _memoryStream = new MemoryStream();
  
    public bool Converto(Stream stream)
                  {
                      try
                      {
                          copyStream(stream);
                          //more code
                      }
                      catch (IOException ex)
                      {
                          Debug.WriteLine(ex);
                      }
  
                      return true;
                  }
  
  
         private void copyStream(Stream stream)
          {
              stream.CopyTo(_memoryStream); //here it throws the error
          }
  }
  </code></pre>
  
  <p><strong>p.s. I search for this error here before posting, but non of the topics helped me.</strong></p>
  
  <blockquote>
    <p>SOLVED by restarting PC, the code it ok.</p>
  </blockquote>