How to create a Binary File using C#

The System.IO.BinaryWriter class is used to write binary data to a stream. This class also provides an option to specify the character encoding including ASCII, Unicode, UTF32, UTF7, and UTF8 encoding. 
 
Create a BinaryWriter
 
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. string fileName = @"C:\temp\MC.bin";                  
  2. BinaryWriter bwStream = new BinaryWriter(new FileStream(fileName,FileMode.Create));  
  3.   
  4. Encoding ascii = Encoding.ASCII;  
  5. BinaryWriter bwEncoder = new BinaryWriter(new FileStream(fileName, FileMode.Create), ascii);  
Writing Binary to a Stream
 
The Write 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 following code snippet creates a BinaryWriter and writes string, Boolean, integer and double data types to the stream. 
  1. string authorName = "Mahesh Chand";  
  2. int age = 30;  
  3. string bookTitle = "ADO.NET Programming using C#";  
  4. bool mvp = true;  
  5. double price = 54.99;  
  6.   
  7. using (BinaryWriter binWriter =  
  8.     new BinaryWriter(File.Open(fileName, FileMode.Create)))  
  9. {  
  10.     // Write string   
  11.     binWriter.Write(authorName);  
  12.     // Write string   
  13.     // Write integer  
  14.     binWriter.Write(age);  
  15.     binWriter.Write(bookTitle);  
  16.     // Write boolean  
  17.     binWriter.Write(mvp);  
  18.     // Write double   
  19.     binWriter.Write(price);  
  20. }  
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. }