C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# BinaryWriter Class: Write Binary Data

In this tutorial, we'll demonstrate how to use the BinaryWriter class in C# to write binary data to a file. The BinaryWriter class is part of the System.IO namespace and provides methods for writing binary data to a stream.

  • Set up the environment

Create a new C# Console Application project in Visual Studio and add the following namespaces:

using System.IO;
using System.Text;
  • Write binary data to a file

To write binary data to a file, use the BinaryWriter class. The following code writes a string, an integer, a double, and a boolean to a binary file named data.bin:

using (FileStream fs = new FileStream("data.bin", FileMode.Create))
{
    using (BinaryWriter bw = new BinaryWriter(fs, Encoding.UTF8))
    {
        bw.Write("Hello, BinaryWriter!");
        bw.Write(42);
        bw.Write(3.14);
        bw.Write(true);
    }
}

In the example above, we first create a FileStream with the file mode set to Create, which creates a new file or overwrites an existing one. Then, we create a BinaryWriter instance, passing the file stream and encoding as parameters.

We use the Write method of the BinaryWriter class to write various data types to the binary file.

  • Read the binary data from the file

To verify that the binary data was written correctly, you can use the BinaryReader class to read the data from the data.bin file:

using (FileStream fs = new FileStream("data.bin", FileMode.Open))
{
    using (BinaryReader br = new BinaryReader(fs, Encoding.UTF8))
    {
        string text = br.ReadString();
        int number = br.ReadInt32();
        double value = br.ReadDouble();
        bool flag = br.ReadBoolean();

        Console.WriteLine($"Text: {text}");
        Console.WriteLine($"Number: {number}");
        Console.WriteLine($"Value: {value}");
        Console.WriteLine($"Flag: {flag}");
    }
}

When you run the Console Application, you should see the following output:

Text: Hello, BinaryWriter!
Number: 42
Value: 3.14
Flag: True

In this tutorial, we've shown you how to use the BinaryWriter class in C# to write binary data to a file. The BinaryWriter class provides methods for writing binary data to a stream, making it easy to write different data types to binary files.

  1. C# BinaryWriter example:

    • Description: This example introduces the BinaryWriter class in C#, which provides methods for writing binary data to streams.
    • Code:
      using (FileStream fileStream = new FileStream("binaryfile.bin", FileMode.Create))
      using (BinaryWriter binaryWriter = new BinaryWriter(fileStream))
      {
          // Write binary data using BinaryWriter methods
          binaryWriter.Write(42);
          binaryWriter.Write(3.14);
          // ... continue writing as needed
      }
      
  2. Seeking and navigating in binary files with BinaryWriter C#:

    • Description: Explaining how to use the Seek method of BinaryWriter to navigate to a specific position in a binary file.
    • Code:
      using (FileStream fileStream = new FileStream("binaryfile.bin", FileMode.Create))
      using (BinaryWriter binaryWriter = new BinaryWriter(fileStream))
      {
          // Seek to a specific position in the file
          binaryWriter.Seek(8, SeekOrigin.Begin);
      
          // Write data to the new position
          binaryWriter.Write(42);
      }