Windows 8 continues to amaze me. I recently upgraded to the Windows 8 Release Preview and tried to run one of our projects. After dealing with some of the changes, it returned an error that I could not quite place.
Debugging revealed that stream.ReadByte() would sometimes leave the stream position far beyond the end of the stream. This only happened in combination with a call to stream.Seek().
So, I wrote the following test. It opens a file, and then tries to seek increasingly further in it – but not beyond the end - and at all these positions it reads a single byte. As soon as this leads to a position beyond the end of the stream, it prints how far from the end of the stream we were actually trying to read.
var file = await fileOpenPicker.PickSingleFileAsync();
if (file != null)
Stream stream = await file.OpenStreamForReadAsync();
for (long i = 0; i < stream.Length; i++)
if (stream.ReadByte() >= 0)
if (stream.Position >= stream.Length)
System.Diagnostics.Debug.WriteLine(stream.Length - i);
And the result is: things start to go wrong at 8192 bytes before the end of the stream. For every file I tested. Always.
Now I am starting to become a bit paranoid. Is it just me? Am I doing something wrong? Is my metro system hosed? I have Googled for this issue, but found nothing. Am I the only one trying to do these things on Windows 8? I cannot believe it.
Author: Marco Kesseler