如何阅读在Windows商店应用二进制文件? [英] How do I read a binary file in a Windows Store app?
问题描述
如何读取在Windows商店应用二进制文件,或者更具体地说我该如何创建我流时,System.IO命名空间不包含任何文件类?
How can I read a binary file in a Windows Store app, or more specifically how can I create my Stream, when the System.IO namespace contains no File class?
借助文档为BinaryReader例子帮倒忙使用文件!
The documentation examples for BinaryReader unhelpfully use File!
推荐答案
您总是访问在Windows应用商店中的应用程序文件中使用的 StorageFile 类:
You always access files in Windows Store apps using StorageFile class:
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);
您就可以开始使用的WinRT API的文件的二进制内容:
You can then get the binary contents of the file using the WinRT APIs:
IBuffer buffer = await FileIO.ReadBufferAsync(file);
byte[] bytes = buffer.ToArray();
如果你想使用的 BinaryReader 你需要一个流来代替:
If you want to use BinaryReader you need a stream instead:
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("a");
Stream stream = (await file.OpenReadAsync()).AsStreamForRead();
BinaryReader reader = new BinaryReader(stream);
请确认您使用的的ReadBytes()以在这种情况下,不考虑编码到二进制数据。
Make sure you only use ReadBytes() for binary data in this case which doesn't take encoding into account.
这篇关于如何阅读在Windows商店应用二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!