Copiare il contenuto di uno stream in un altro

Scritto da  Luca Cestola il lunedì 22 novembre 2010  •  Linguaggio: C#,VB   • Livello: 100


Con il framework 4.0 è stato introdotto il metodo CopyTo della classe Stream che copia il contenuto di uno stream su un altro. Con i framework precedenti possiamo utilizzare un codice di questo tipo:

C#

public static void CopyToStream(Stream input, Stream output)
{
byte[] buffer = new byte[4096];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}

 

VB.Net

Public Sub CopyToStream(ByVal input As Stream, ByVal output As Stream)
Dim buffer(4095) As Byte
Dim read As Integer = input.Read(buffer, 0, buffer.Length)
Do While (read > 0)
output.Write(buffer, 0, read)
read = input.Read(buffer, 0, buffer.Length)
Loop
End Sub


Tags: VB.NET,C#

 
x