在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
(orWinRT
or windows store application or however it is called, I'm completely confused),System.IO
has been replaced withWindows.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
最满意答案
“发生了什么”是按顺序比较两个字典的
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 ...
-
比较C#中的两个字典(Comparing two Dictionaries in C#)[2022-04-09]
“发生了什么”是按顺序比较两个字典的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) ... -
C#使用字典(C# using dictionaries)[2023-07-10]
那么我认为你到目前为止做得非常好。 不能在这里使用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 ...
-
C#中的字典(Dictionaries in C#)[2022-06-07]
您正在为每个循环周期创建一个新的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 ...
-
C#处理字典(C# Dealing with Dictionaries)[2024-02-12]
dictionary1.ContainsKey((object)"1111")永远不会返回true,因为"1111"每次都会被装入一个新的唯一对象。 一次填充一个项目 您可以一次填充一个项目的字典: Dictionary