Cómo convertir el Sistema struct.Byte byte [] a un objeto System.IO. Stream en C#?


Cómo convertir una estructura System.Byte byte[] ¿a un objeto System.IO.Stream en C#?

Author: Olorunfemi Ajibulu, 2011-01-19

4 answers

La forma más fácil de convertir una matriz de bytes en una secuencia es utilizando el MemoryStream clase:

Stream stream = new MemoryStream(byteArray);
 978
Author: Martin Buberl,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-02-23 10:59:00

Estás buscando el MemoryStream.Write método. Por ejemplo, el siguiente código escribirá el contenido de una matriz byte[] en un flujo de memoria:

byte[] myByteArray = new byte[10];
MemoryStream stream = new MemoryStream();
stream.Write(myByteArray, 0, myByteArray.Length);

Alternativamente, podría crear un nuevo, objeto MemoryStream no redimensionable basado en la matriz de bytes:

byte[] myByteArray = new byte[10];
MemoryStream stream = new MemoryStream(myByteArray);
 284
Author: Cody Gray,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-01-19 14:11:14

El enfoque general para escribir en cualquier flujo (no solo MemoryStream) es usar BinaryWriter:

static void Write(Stream s, Byte[] bytes)
{
    using (var writer = new BinaryWriter(s))
    {
        writer.Write(bytes);
    }
}
 25
Author: QrystaL,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-09 20:14:18

Buscar en el MemoryStream clase.

 4
Author: Corey Ogburn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-20 05:03:02