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. 

string fileName = @"C:\temp\MC.bin";                
BinaryWriter bwStream = new BinaryWriter(new FileStream(fileName,FileMode.Create));

Encoding ascii = Encoding.ASCII;
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. 

string authorName = "Mahesh Chand";
int age = 30;
string bookTitle = "ADO.NET Programming using C#";
bool mvp = true;
double price = 54.99;

using (BinaryWriter binWriter =
    new BinaryWriter(File.Open(fileName, FileMode.Create)))
{
    // Write string 
    binWriter.Write(authorName);
    // Write string 
    // Write integer
    binWriter.Write(age);
    binWriter.Write(bookTitle);
    // Write boolean
    binWriter.Write(mvp);
    // Write double 
    binWriter.Write(price);
}              

Sample 

Here is a complete sample code that creates a binary file and adds data to it.

public class BinaryReadWriteClass
{
    public void WriteBinary()
    {
        try
        {
            Console.WriteLine("Binary Writer");
            string authorName = "Mahesh Chand";
            int age = 30;
            string bookTitle = "ADO.NET Programming using C#";
            bool mvp = true;
            double price = 54.99;

            string fileName = @"C:\temp\MC.bin";                
            BinaryWriter bwStream = new BinaryWriter(new FileStream(fileName,FileMode.Create));

            Encoding ascii = Encoding.ASCII;
            BinaryWriter bwEncoder = new BinaryWriter(new FileStream(fileName, FileMode.Create), ascii);

            using (BinaryWriter binWriter =
                new BinaryWriter(File.Open(fileName, FileMode.Create)))
            {
                // Write string 
                binWriter.Write(authorName);
                // Write string 
                // Write integer
                binWriter.Write(age);
                binWriter.Write(bookTitle);
                // Write boolean
                binWriter.Write(mvp);
                // Write double 
                binWriter.Write(price);
            }              
            Console.WriteLine("Data Written!");
            Console.WriteLine();
        }
        catch (IOException ioexp)
        {
            Console.WriteLine("Error: {0}", ioexp.Message);
        }
    }
}

Up Next
    Ebook Download
    View all
    Learn
    View all