C# BinaryReader ExampleUse BinaryReader and the ReadInt32 method to read the data from a binary file as integers.
This C# class handles binary files. A binary file may have thousands of integers stored in it, or another simple data type. Many files can be treated as binary.
File details. If you do not have a binary file you are trying to open already, you can create one using the BinaryWriter type. BinaryReader makes using binary data easier.
Here we open the same binary file and then read in the bytes. BinaryReader here provides some useful properties. When you run the next program, you will see all the ints recovered.
FileStream We see that BinaryReader receives a FileStream. This is returned by File.Open.
ReadInt32 The program calls ReadInt32. This method consumes 4 bytes in the binary file and turns it into an int.
Also The method advances the file read. It increments our position. This line adds 4 bytes to our position.
Sizeof We use sizeof int. This evaluates to the integer value 4 and always will, but it might be preferable for clarity.
static void Main()
static void R()
using (BinaryReader b = new BinaryReader
// Position and length variables.
int pos = 0;
// Use BaseStream.
int length = (int)b.BaseStream.Length;
while (pos < length)
// Read integer.
int v = b.ReadInt32
// Advance our position variable.
pos += sizeof(int);
Most programs read in much more data than they write. I found BinaryReader substantially faster than using plain text.
Info Looking into PeekChar indicates that it performs a separate read each time it is called.
Tip This can easily multiply the time required for the BinaryReader. PeekChar should thus be avoided.
Fragment that uses PeekChar, C#:
using (BinaryReader b = new BinaryReader(File.Open("file.bin"
// Do not do this.
while (b.PeekChar() != -1)
int v = b.ReadInt32();
Encoding. Sometimes you can fix an encoding problem by specifying Encoding.ASCII to File.Open. I do not understand why this works but it solved one of my problems.
// If all else fails, specify Encoding.ASCII to your File.Open call.
Example that uses Encoding.ASCII, C#:
using (BinaryReader b = new BinaryReader(
, FileMode.Open, Encoding.ASCII)))
Here we review the methods on BinaryReader. It is important to know the exact size of values to read—if we do not use the right method, results will be incorrect.
ReadBoolean Reads in a true or false value. I haven't used this one, and in many programs int would be used instead.
ReadByte Reads in a single byte. This is useful for images and archives, but ReadBytes is faster.
ReadChar, ReadChars These seem confusing, because they vary on the encoding used. Unicode chars are not the same as ASCII ones.
ReadDecimal Reads in 16 bytes that are a decimal value. More information on the decimal type is available.
ReadInt16, ReadInt32, ReadInt64 2, 4 or 8 bytes are read. I had problems with ReadInt16. I think ReadInt32 is commonly useful.
ReadString Microsoft says the string is "prefixed with the length, encoded as an integers even bits at a time."
Summary. This C# class is effective for reading binary files. Binary files have great advantages for performance, but they tend not to be standard and can be difficult to work with.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.