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

后端 未结 13 2342
悲哀的现实
悲哀的现实 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条回答
  •  -上瘾入骨i
    2020-11-21 22:54

    .NET Framework 4 introduce new "CopyTo" method of Stream Class of System.IO namespace. Using this method we can copy one stream to another stream of different stream class.

    Here is example for this.

        FileStream objFileStream = File.Open(Server.MapPath("TextFile.txt"), FileMode.Open);
        Response.Write(string.Format("FileStream Content length: {0}", objFileStream.Length.ToString()));
    
        MemoryStream objMemoryStream = new MemoryStream();
    
        // Copy File Stream to Memory Stream using CopyTo method
        objFileStream.CopyTo(objMemoryStream);
        Response.Write("

    "); Response.Write(string.Format("MemoryStream Content length: {0}", objMemoryStream.Length.ToString())); Response.Write("

    ");

提交回复
热议问题