2012-03-22 13 views
13

Actualmente intento descargar una pista de audio desde un WCF, necesito ayuda para escribirla en el disco duro, ¿cómo configuro Streamwriter u otro para hacer esto en una aplicación web?Escribiendo un archivo desde StreamReader stream

// Use Service to download stream (returns System.IO.Stream) 
Stream stream = MyService.Download(("1231")); 
// ReadStream 
StreamReader reader = new StreamReader(stream); 
reader.ReadToEnd(); 

// Write this stream to a file/Hard disk 
??? 
+1

StreamReader es para datos de texto – Reniuz

+0

woops -_- sólo se ocupaba de texto en las corrientes antes de esto, gracias. – RY4N

Respuesta

24
Stream stream = MyService.Download(("1231")); 
using (Stream s = File.Create(path)) 
{ 
    stream.CopyTo(s); 
} 
+0

Así es como debe hacerse, pero no funcionará con su código a menos que use algo más que StreamReader o haga algo como 'var reader = new StreamReader (stream, Encoding.GetEncoding (28591));' – Showtime

+0

¿Usted ¿sabes cómo hacer lo mismo en .Net 3.5? – Fayilt

+1

@Fayilt con un bucle. 'void CopyTo (Stream from, Stream to) { byte [] buffer = new byte [0x10000]; int read = from.Read (buffer, 0, buffer.Length); while (leer> 0) { to.Write (buffer, 0, leer); read = from.Read (buffer, 0, buffer.Length); } } ' –

Cuestiones relacionadas