title: File IO in Windows 8
body: <p>I have been trying to read a file, and calculate the hash of the contents to find duplicates. The problem is that in <code>Windows 8</code> (or <code>WinRT</code> or windows store application or however it is called, I'm completely confused), <code>System.IO</code> has been replaced with <code>Windows.Storage</code>, which behaves differently, and is very confusing. The official documentation is not useful at all.</p>
  
  <p>First I need to get a StorageFile object, which in my case, I get from browsing a folder from a file picker:</p>
  
  <pre><code>var picker = new Windows.Storage.Pickers.FolderPicker();
  picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.MusicLibrary;
  picker.FileTypeFilter.Add("*");
  var folder = await picker.PickSingleFolderAsync();
  var files = await folder.GetFilesAsync(Windows.Storage.Search.CommonFileQuery.OrderByName);
  </code></pre>
  
  <p>Now in files I have the list of files I need to index. Next, I need to open that file:</p>
  
  <pre><code>foreach (StorageFile file in files)
  {
      var filestream = file.OpenAsync(Windows.Storage.FileAccessMode.Read);
  </code></pre>
  
  <p>Now is the most confusing part: getting the data from the file. The documentation was useless, and I couldn't find any code example. Apparently, Microsoft thought getting pictures from the camera is more important than opening a file.</p>
  
  <p>The file stream has a member <code>ReadAsync</code> which I think reads the data. This method needs a buffer as a parameter and returns another buffer (???). So I create a buffer:</p>
  
  <pre><code>    var buffer = new Windows.Storage.Streams.Buffer(1024 * 1024 * 10); // 10 mb should be enough for an mp3
      var resultbuffer = await filestream.ReadAsync(buffer, 1024 * 1024 * 10, Windows.Storage.Streams.InputStreamOptions.ReadAhead);
  </code></pre>
  
  <p>I am wondering... what happens if the file doesn't have enough bytes? I haven't seen any info in the documentation.</p>
  
  <p>Now I need to calculate the hash for this file. To do that, I need to create an algorithm object...</p>
  
  <pre><code>    var alg = Windows.Security.Criptography.Core.HashAlgorithmProvider.OpenAlgorithm("md5");
      var hashbuff = alg.HashData(resultbuffer);
  
      // Cleanup
      filestream.Dispose();
  </code></pre>
  
  <p>I also considered reading the file in chunks, but how can I calculate the hash like that? I looked everywhere in the documentation and found nothing about this. Could it be the CryptographicHash class type with it's 'append' method?</p>
  
  <p>Now I have another issue. How can I get the data from that weird buffer thing to a byte array? The IBuffer class doesn't have any 'GetData' member, and the documentation, again, is useless.</p>
  
  <p>So all I could do now is wonder about the mysteries of the universe...</p>
  
  <pre><code>    // ???
  }
  </code></pre>
  
  <p>So the question is... how can I do this? I am completely confused, and I wonder why did Microsoft choose to make reading a file so... so... so... impossible! Even in Assembly I could figure it out easier than.... this thing.</p>