Windows 8 IO File

I am trying to read a file and compute a hash of content to find duplicates. The problem is that in Windows 8 (or WinRT or a Windows storage application or, nevertheless, it is called, I am completely confused), System.IO been replaced by Windows.Storage , which behaves differently and is very confusing. The official documentation is generally not useful.

First I need to get the StorageFile object, which in my case I get from viewing the folder from the file collector:

 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); 

Now in the files I have a list of files that I need to index. Then I need to open this file:

 foreach (StorageFile file in files) { var filestream = file.OpenAsync(Windows.Storage.FileAccessMode.Read); 

Now the most confusing part: getting data from a file. The documentation was useless and I could not find the code. Apparently, Microsoft thought that taking pictures from a camera was more important than opening a file.

There is a ReadAsync member in the file stream, which I think reads the data. This method needs a buffer as a parameter and returns another buffer (???). Therefore, I create a buffer:

  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); 

I am wondering ... what happens if there are not enough bytes in the file? I did not see any information in the documentation.

Now I need to calculate the hash for this file. To do this, I need to create an algorithm object ...

  var alg = Windows.Security.Criptography.Core.HashAlgorithmProvider.OpenAlgorithm("md5"); var hashbuff = alg.HashData(resultbuffer); // Cleanup filestream.Dispose(); 

I also considered reading a file in chunks, but how can I calculate a hash like this? I searched everywhere for documentation and found nothing about it. Could there be a class of the CryptographicHash class with its add method?

Now I have one more problem. How can I get data from this strange buffer thing into an array of bytes? The IBuffer class does not have a 'GetData' member, and the documentation is useless again.

So, all I could do now is wonder about the secrets of the universe ...

  // ??? } 

So the question is ... how can I do this? I am completely confused, and I wonder why Microsoft decided to make reading the file so ... so ... so ... impossible! Even in the Assembly, I could understand this easier than ... this thing.

+4
source share
1 answer

WinRT or Windows Runtime should not be confused with .NET, as it is not .NET. WinRT has access only to a subset of the Win32 API, but not to everything that relates to .NET. Here is a pretty good article on what are the rules and limitations in WinRT.

WinRT generally does not have access to the file system. It works with features, and you can allow access to the file, but this will restrict your application to only certain areas. Here is a good example of how to access a file through WinRT.

+2
source

Source: https://habr.com/ru/post/1446461/


All Articles