How To Convert System.Byte To A Object Using C#

In this post, we will learn about how to convert bytes to stream using C# console application. In this example, first, we read all the bytes from the file using File.ReadAllBytes method. Then using MemoryStream we add all bytes into memory stream. For reading bytes one by one we are using BinaryReader using for loop. There is another option for converting byte to memory stream or stream using C#. Let's start coding.
Method 1

Read all bytes from the file then convert it into MemoryStream and again convert into BinaryReader for reading each byte of the array. 
  1. byte[] file = File.ReadAllBytes("{FilePath}");  
  3. using (MemoryStream memory = new MemoryStream(file))  
  4. {  
  5.     using (BinaryReader reader = new BinaryReader(memory))  
  6.     {  
  7.         for (int i = 0; i < file.Length; i++)  
  8.         {  
  9.             byte result = reader.ReadByte();  
  10.             Console.WriteLine(result);  
  11.         }  
  12.     }  

Method 2

Read all bytes from the file and convert it into Stream:
  1. byte[] file = File.ReadAllBytes("{FilePath}");  
  3. Stream stream = new MemoryStream(file);