O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

BinaryReader and BinaryWriter

To go beyond raw byte sequences when dealing with I/O data, the BCL designers invented the BinaryReader and BinaryWriter classes. Just as StreamReader and StreamWriter can be constructed by passing in a Stream object to a constructor, this abstraction simplifies the use of streams by providing additional methods. For example, where StreamReader concerns itself with the notion of textual data and provides a ReadLine method, the BinaryReader class exposes Read methods for various primitive multibyte data types.

Let’s look at an example, again using binary files:

using (var fs = File.OpenRead(@"c:\windows\system32\notepad.exe"))using (var br = new BinaryReader(fs)){    br.BaseStream.Position ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required