How to create a Binary File in C#

The System.IO.BinaryWriter class writes 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 writes primitive data types to a stream. The Write method can write 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);  
}  

Complete Code Example

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

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);  
        }  
    }  
}

Summary

This code example demonstrated how to create a binary file in C#.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.