{"id":4186,"date":"2014-03-30T09:08:11","date_gmt":"2014-03-30T09:08:11","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/file-io-in-windows-8-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:08:11","modified_gmt":"2014-03-30T09:08:11","slug":"file-io-in-windows-8-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/file-io-in-windows-8-collection-of-common-programming-errors\/","title":{"rendered":"File IO in Windows 8-Collection of common programming errors"},"content":{"rendered":"<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&#8217;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>\n<p>First I need to get a StorageFile object, which in my case, I get from browsing a folder from a file picker:<\/p>\n<pre><code>var picker = new Windows.Storage.Pickers.FolderPicker();\npicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.MusicLibrary;\npicker.FileTypeFilter.Add(\"*\");\nvar folder = await picker.PickSingleFolderAsync();\nvar files = await folder.GetFilesAsync(Windows.Storage.Search.CommonFileQuery.OrderByName);\n<\/code><\/pre>\n<p>Now in files I have the list of files I need to index. Next, I need to open that file:<\/p>\n<pre><code>foreach (StorageFile file in files)\n{\n    var filestream = file.OpenAsync(Windows.Storage.FileAccessMode.Read);\n<\/code><\/pre>\n<p>Now is the most confusing part: getting the data from the file. The documentation was useless, and I couldn&#8217;t find any code example. Apparently, Microsoft thought getting pictures from the camera is more important than opening a file.<\/p>\n<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>\n<pre><code>    var buffer = new Windows.Storage.Streams.Buffer(1024 * 1024 * 10); \/\/ 10 mb should be enough for an mp3\n    var resultbuffer = await filestream.ReadAsync(buffer, 1024 * 1024 * 10, Windows.Storage.Streams.InputStreamOptions.ReadAhead);\n<\/code><\/pre>\n<p>I am wondering&#8230; what happens if the file doesn&#8217;t have enough bytes? I haven&#8217;t seen any info in the documentation.<\/p>\n<p>Now I need to calculate the hash for this file. To do that, I need to create an algorithm object&#8230;<\/p>\n<pre><code>    var alg = Windows.Security.Criptography.Core.HashAlgorithmProvider.OpenAlgorithm(\"md5\");\n    var hashbuff = alg.HashData(resultbuffer);\n\n    \/\/ Cleanup\n    filestream.Dispose();\n<\/code><\/pre>\n<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&#8217;s &#8216;append&#8217; method?<\/p>\n<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&#8217;t have any &#8216;GetData&#8217; member, and the documentation, again, is useless.<\/p>\n<p>So all I could do now is wonder about the mysteries of the universe&#8230;<\/p>\n<pre><code>    \/\/ ???\n}\n<\/code><\/pre>\n<p>So the question is&#8230; how can I do this? I am completely confused, and I wonder why did Microsoft choose to make reading a file so&#8230; so&#8230; so&#8230; impossible! Even in Assembly I could figure it out easier than&#8230;. this thing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been trying to read a file, and calculate the hash of the contents to find duplicates. The problem is that in Windows 8 (or WinRT or windows store application or however it is called, I&#8217;m completely confused), System.IO has been replaced with Windows.Storage, which behaves differently, and is very confusing. The official documentation [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4186","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4186"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4186\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}