在Windows 8中的文件IO(File IO in Windows 8)

编程入门 行业动态 更新时间:2024-10-27 18:32:07
在Windows 8中的文件IO(File IO in Windows 8)

我一直在尝试读取文件,并计算内容的散列以查找重复项。 问题是在Windows 8 (或WinRT或windows store应用程序,或者它被调用,我完全困惑), System.IO已被替换为Windows.Storage ,它的行为不同,并且非常混乱。 官方文件根本没用。

首先,我需要得到一个StorageFile对象,在我的情况下,我通过从文件选取器中浏览文件夹来获得这个对象:

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

现在在文件中我有我需要索引的文件列表。 接下来,我需要打开该文件:

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

现在是最容易混淆的部分:从文件中获取数据。 该文档是无用的,我找不到任何代码示例。 显然,微软认为从照相机获取照片比打开文件更重要。

文件流有一个我认为读取数据的成员ReadAsync 。 该方法需要一个缓冲区作为参数并返回另一个缓冲区(???)。 所以我创建了一个缓冲区:

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

我想知道...如果文件没有足够的字节会发生什么? 我在文档中没有看到任何信息。

现在我需要计算这个文件的哈希值。 要做到这一点,我需要创建一个算法对象...

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

我也考虑过以块的形式读取文件,但我如何计算这样的散列? 我在文档中无处不在,对此一无所知。 它是'append'方法的CryptographicHash类型吗?

现在我有另一个问题。 我怎么能从奇怪的缓冲区的东西获取数据到一个字节数组? IBuffer类没有任何'GetData'成员,并且文档也是无用的。

所以我现在所能做的只是想知道宇宙的奥秘......

// ??? }

所以问题是......我该怎么做? 我完全困惑,我想知道为什么微软选择阅读一个文件,所以......所以......不可能! 即使在大会上,我也能想出比这件事更容易的事情。

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'm completely confused), System.IO has been replaced with Windows.Storage, which behaves differently, and is very confusing. The official documentation is not useful at all.

First I need to get a StorageFile object, which in my case, I get from browsing a folder from a file picker:

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 files I have the list of files I need to index. Next, I need to open that file:

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

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.

The file stream has a member ReadAsync which I think reads the data. This method needs a buffer as a parameter and returns another buffer (???). So 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 the file doesn't have enough bytes? I haven't seen any info in the documentation.

Now I need to calculate the hash for this file. To do that, 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 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?

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.

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

// ??? }

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.

最满意答案

WinRT或Windows运行时不应与.NET混淆,因为它不是.NET。 WinRT只能访问Win32 API的一个子集,但不能访问.NET等所有内容。 这里有一篇很好的文章,介绍WinRT中的规则和限制 。

WinRT通常无法访问文件系统。 它适用于功能 ,您可以允许文件访问功能,但这会限制您的应用只能访问某些区域。 以下是如何通过WinRT进行文件访问的一个很好的例子。

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

The WinRT in general does not have access to the file system. It works with capabilities and you can allow file access capability but this would restrict your app's access only to certain areas. Here is a good example of how do to file access via WinRT.

更多推荐

本文发布于:2023-07-31 11:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1343804.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   Windows   File   IO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!