find and solve || findandsolve.com
Please wait.....

How to use C# BinaryWriter Class

In the C#, BinaryWriter is a class of System.IO namespace which is sworks at lower level of Streams. It is using for write primitive types as binary values in a specific encoding stream.

C# BinaryWriter Example

using System;  
using System.IO;
namespace BinaryWriterExample {
   public class Program
    {
        static void Main(string[] args)
        {
            string fileName = "d:\\test.dat";
            using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
            {
                writer.Write(2.5);
                writer.Write("this is string data");
                writer.Write(true);
            }
            Console.WriteLine("Data written successfully...");
        }
    }
}  

Output

Data written successfully...