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#.

Mariusz PostolPosted Aug 3, 2024, 1:36 PM
You said: "This code example demonstrated how to create a binary file in C#." let me ask what is the difference between binary and not binary files. According to my experience gathered many years ago in WD, all files must have binary content because the disk is a binary machine. I found this article while working on a similar question at: <https://www.c-sharpcorner.com/forums/binary-coding-system>. I hope that the correct answer we can derive from the next one: what condition must be held that a binary file becomes a text file?
Pablo AMPosted Jan 21, 2021, 1:37 PM
Hello, this is not working I got this:ioexp.Message = "The process cannot access the file 'C:\\temp\\MC.bin' because it is being used by another process."