首页 \ 问答 \ 在Windows 8中的文件IO(File IO in Windows 8)

在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.


原文:https://stackoverflow.com/questions/13418037
更新时间:2023-07-19 10:07

最满意答案

“发生了什么”是按顺序比较两个字典的KeyValuePair条目。 字典本质上是无序的 - 你不应该依赖任何有关条目出现的顺序。 如果你使用:

firstDictionary.OrderBy(pair => pair.Key)
               .SequenceEqual(secondDictionary.OrderBy(pair => pair.Key))

我怀疑你会找到那些比赛。 这是一个相当不愉快的方式来比较他们虽然:)


"What's going on" is it's comparing KeyValuePair entries for the two dictionaries, in order. Dictionaries are inherently unordered - you shouldn't be relying on anything about the order in which entries come out of them. If you use:

firstDictionary.OrderBy(pair => pair.Key)
               .SequenceEqual(secondDictionary.OrderBy(pair => pair.Key))

I suspect you'll find that matches. It's a pretty unpleasant way to compare them though :)

相关问答

更多
  • 首先,您必须在您的类中重写Equals和GetHashCode方法,否则将在引用上执行比较而不是实际值。 (最后提供覆盖Equals和GetHashCode的代码) ,然后您可以使用: var result = (dic1 == dic2) || //Reference comparison (if both points to same object) (dic1.Count == dic2.Count && !dic1.Except(dic2).Any()); 由于Dicti ...
  • “发生了什么”是按顺序比较两个字典的KeyValuePair条目。 字典本质上是无序的 - 你不应该依赖任何有关条目出现的顺序。 如果你使用: firstDictionary.OrderBy(pair => pair.Key) .SequenceEqual(secondDictionary.OrderBy(pair => pair.Key)) 我怀疑你会找到那些比赛。 这是一个相当不愉快的方式来比较他们虽然:) "What's going on" is it's compar ...
  • 扫描一个字典并在第二个查找并比较如下: var a = new Dictionary(); var b = new Dictionary(); var isEquals = a.All(x => { TV v; if (b.TryGetValue(x.Key, out v)) return x.Value.Equals(v); return false; }); UPD isEquals = true; foreach(x in a) ...
  • 那么我认为你到目前为止做得非常好。 不能在这里使用Linq是折磨;) 至于性能,你应该考虑让你的SecondFileWords成为一个HashSet因为如果第二个文件中存在任何单词,这将会增加查找时间,而不需要太多的努力。 如果性能不是关键要求,那么就性能优化而言,我不会进一步讨论这样的练习。 当然,你必须检查你是否在第二个列表中添加了重复项,所以将当前的实现改为如下所示: HashSet temp2 = new HashSet(); foreach (va ...
  • 测试字典是否具有相同的值(但是,可能是不同的键) public static Boolean DictionaryHaveEqualValues(IDictionary left, IDictionary right) { if (Object.ReferenceEquals(left, right)) return true; else if (Object.ReferenceEquals(left, null)) return false; else if (Object.R ...
  • from collections import Counter c1 = Counter({'item1': 4, 'item2':2, 'item3': 5, 'item4': 3}) c2 = Counter({'item1': 6, 'item2':2, 'item3': 1, 'item5': 9}) c3 = {} for c in (c1, c2): for k,v in c.iteritems(): c3.setdefault(k, []).append(v) c3现 ...
  • def checkList(lst): for item in lst: if "A" not in item.keys(): print item checkList(listOne) checkList(listTwo) Ok, i figured it out how to solve it (in a very, very ugly way): listOne = [{'A':'a','B':'b','C':'c'},{'A':'aa','B':'b ...
  • 您正在为每个循环周期创建一个新的Dictionary实例,每次读取一行时基本上都会覆盖它。 将此行移出循环: // Instantiate a dictionary var map = new Dictionary(); 另外为什么不加载字典一次,你加载每个按钮点击,这是不高效的。 ( >=.NET 3 )使用LINQ ToDictionary() : usign System.Linq; var map = File.ReadAllLines() ...
  • 要继续您的重复丢弃方式,只需分组并在组中获取获胜项目(例如最后一项)。 first.Concat(second) .GroupBy(kvp => kvp.Key, kvp => kvp.Value) .ToDictionary(g => g.Key, g => g.Last()); To continue your duplicate discarding ways, just group up and take a winning item in the group (such as the L ...
  • dictionary1.ContainsKey((object)"1111")永远不会返回true,因为"1111"每次都会被装入一个新的唯一对象。 一次填充一个项目 您可以一次填充一个项目的字典: Dictionary dictionary1 = new Dictionary(); for (int i = 0; i < array1.Length; i++) { dictionary1.Add(ar ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。