Create MemoryStream from byte array - CSharp File IO

CSharp examples for File IO:MemoryStream

Description

Create MemoryStream from byte array

Demo Code

using System;/*www.  j a  v a  2  s  .c  o m*/
using System.IO;
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 ms = new MemoryStream(buffer);
      ms.Seek(0, SeekOrigin.Begin);
      Console.WriteLine("Stream length: {0}", ms.ToArray().Length);
      Console.WriteLine("Write the bytes in input buffer to the console:");
      foreach (byte b in buffer)
      {
         Console.Write(b);
      }
   }
}

Result


Related Tutorials