Writing Binary to a Stream
The Write overloaded method is used to write 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);
}
Learn More: Create a binary file in C#