Read a binary file in C#

The System.IO.BinaryReader class is used to read primitive .NET data types as binary in a specific encoding including ASCII, Unicode, UTF32, UTF7, and UTF8 encoding.
 

Create a BinaryReader

 
The BinaryWriter constructor has overloaded forms to support a stream and encoding.  The following code snippet creates BinaryWriter objects with a stream and character encoding format. 
  1. BinaryReader binReader = new BinaryReader(File.Open(fileName, FileMode.Open)  
  2.   
  3. Encoding ascii = Encoding.ASCII;  
  4. BinaryWriter bwEncoder = new BinaryWriter(new FileStream(fileName, FileMode.Create), ascii);  

Reading a Binary File

 
The Read overloaded method is used to write primitive data types to a stream. The Write method can write a Boolean, Byte, Char, Decimal, Double, and Integer data types.  The BinaryReader also provides data type specific methods such as ReadString, ReadInt32 and so on. 
 
The following code snippet reads various data types using their respective methods.
  1. string authorName = binReader.ReadString();  
  2. int age = binReader.ReadInt32();  
  3. string bookTitle = binReader.ReadString();  
  4. bool mvp = binReader.ReadBoolean();  
  5. double price = binReader.ReadDouble();  
Sample
 
Here is a complete sample code that creates a binary file and adds data to it.
  1. public class BinaryReadWriteClass  
  2. {  
  3.     public void WriteBinary()  
  4.     {  
  5.         try  
  6.         {  
  7.             Console.WriteLine("Binary Writer");  
  8.             string authorName = "Mahesh Chand";  
  9.             int age = 30;  
  10.             string bookTitle = "ADO.NET Programming using C#";  
  11.             bool mvp = true;  
  12.             double price = 54.99;  
  13.   
  14.             string fileName = @"C:\temp\MC.bin";                  
  15.             BinaryWriter bwStream = new BinaryWriter(new FileStream(fileName,FileMode.Create));  
  16.   
  17.             Encoding ascii = Encoding.ASCII;  
  18.             BinaryWriter bwEncoder = new BinaryWriter(new FileStream(fileName, FileMode.Create), ascii);  
  19.   
  20.             using (BinaryWriter binWriter =  
  21.                 new BinaryWriter(File.Open(fileName, FileMode.Create)))  
  22.             {  
  23.                 // Write string   
  24.                 binWriter.Write(authorName);  
  25.                 // Write string   
  26.                 // Write integer  
  27.                 binWriter.Write(age);  
  28.                 binWriter.Write(bookTitle);  
  29.                 // Write boolean  
  30.                 binWriter.Write(mvp);  
  31.                 // Write double   
  32.                 binWriter.Write(price);  
  33.             }                
  34.             Console.WriteLine("Data Written!");  
  35.             Console.WriteLine();  
  36.         }  
  37.         catch (IOException ioexp)  
  38.         {  
  39.             Console.WriteLine("Error: {0}", ioexp.Message);  
  40.         }  
  41.     }  
  42. }