In c#, BinaryWriter is a class of System.IO namespace and it is useful to write binary information to the stream with a particular encoding. By default, the BinaryWriter will use UTF-8 Encoding unless we specify other encodings.
Now, we will see how to use BinaryWriter class in c# to write binary information to the file with examples.
Following is the example of writing a text to the file in binary form using BinaryWriter object in c#.
static void Main(string args)
string fpath = @"D:\Test.txt";
// Check file if exists
using (BinaryWriter bw = new BinaryWriter(File.Open(fpath,FileMode.Create)))
bw.Write("Welcome to Tutlane");
When we execute the above example, it will create a new “Test.txt” file in D drive with required text like as shown below.
This is how we can create, delete and add text to files using File, FileStream & BinaryWriter objects in c#.