.Net Framework
C# Tutorial
C# Control Statement
C# Arrays
C# Object Class
C# Properties
C# Inheritance
C# Polymorphism
C# Abstraction
C# Strings
C# Exception Handling
C# File IO
C# Collections
C# Generics
C# Delegates
C# Reflection
Anonymous Function
C# Multithreading
C# Synchronization
C# Web Service
C# Misc
C# New Features
C# Programs
C# Interview Questions
ADO.NET Tutorial
ASP.NET Tutorial
C# BinaryWriter
C# BinaryWriter class is used to write binary information into stream. It is found in System.IO namespace. It also supports writing string in specific encoding.
C# BinaryWriter Example
Let's see the simple example of BinaryWriter class which writes data into dat file.
using System;
using System.IO;
namespace BinaryWriterExample
{
class Program
{
static void Main(string[] args)
{
string fileName = "e:\\binaryfile.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...");
}
}
}
using System.IO;
namespace BinaryWriterExample
{
class Program
{
static void Main(string[] args)
{
string fileName = "e:\\binaryfile.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...