EndOfStream for BinaryReader

melmi picture melmi · Sep 20, 2010 · Viewed 15.5k times · Source

BinaryReader does not have EndOfStream property. Is it safe to use following code to check if end of stream is reached?

reader.BaseStream.Length>reader.BaseStream.Position

Answer

Alex Barac picture Alex Barac · Nov 3, 2010

The easiest way I found is to check the returned value of the BinaryReader's PeekChar() method. If it returns -1, then you reached the end of the stream.