13 Haz

C# Copy Stream Examples

Best ways to copy between two stream are like below. There is no special method for .NET Framework 3.5 and before. We writes a sample method using Read and Write methods in Stream class. But .NET Framework 4.0 and later, some methods are added to framework api.

        /// 
        /// code examples for .NET 3.5 and before
        /// 
        /// 
        /// 
        public static void CopyStream(Stream input, Stream output)
        {
            byte[] buffer = new byte[32768];
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, read);
            }
        }

From .NET 4.0 on, there’s is the Stream.CopyTo method

            Stream inputStream = null;
            Stream outputStream = null;
            inputStream.CopyTo(outputStream);

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir


iki + 4 =