Write Byte array to MemoryStream - CSharp File IO

CSharp examples for File IO:MemoryStream

Description

Write Byte array to MemoryStream

Demo Code


using System;//from   ww w . ja v  a  2s. c o m
using System.IO;
using System.Text;

public class Program
{
    public static void Main(string[] args)
    {
        string data = "test/ntest/ntest/n";  // \n equiv to 2 bytes, so 18 bytes.
        Console.WriteLine("Original data:\n{0}", data);
        byte[] buffer = new byte[data.Length + 20];

        MemoryStream stream2 = new MemoryStream();

        stream2.Write(buffer, 0, buffer.Length);

        stream2.Flush();  // For MemoryStream, this does nothing at all.

        stream2.Close();  // Close the stream.

        foreach (byte b in stream2.ToArray())
        {
            Console.Write(b);
        }
    }
}

Result


Related Tutorials