title: Seeking & reading arbitrary file data in Metro
body: <p>I'm having an inordinate amount of trouble doing something that really should be very simple: Getting arbitrary data from a file in a C# Win8 Metro-style app.</p>
  
  <p>Given a StorageFile object that references an existing file, I need to be able to seek to arbitrary positions and then read arbitrary values (integers of various sizes, strings, bytes, arrays of bytes, whatever). Nothing I've tried has worked. </p>
  
  <p>For example, I obtained an <code>IRandomAccessStream</code> using <code>StorageFile.OpenAsync</code>, constructed a <code>DataReader</code> with it, and used <code>DataReader.ReadByte</code> to read the first byte of the file. But <code>ReadByte</code> throws an exception ("The operation attempted to access data outside the valid range").</p>
  
  <p>It probably doesn't help that I'm relatively new to both C# and .NET.</p>
  
  <p>Can someone outline how to do this?</p>
  
  <hr>
  
  <p>I was asked for a code example. Here is what I described above, one of many different attempts. These may not even be the right types. <code>f</code> is a valid <code>StorageFile</code> that refers to an existing file, picked by the user (so there are no permission issues).</p>
  
  <pre><code>public async void Read(StorageFile f)
  {
    IRandomAccessStream s = await f.OpenAsynch(FileAccessMode.Read);
    DataReader r = new DataReader(s);
    Byte b = r.ReadByte();
  }
  </code></pre>
  
  <p>The <code>ReadByte</code> call is the one that throws the exception.</p>