如何读取 Windows 应用商店应用中的二进制文件? [英] How do I read a binary file in a Windows Store app?
本文介绍了如何读取 Windows 应用商店应用中的二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当 System.IO 命名空间不包含 File 类时,如何读取 Windows 应用商店应用中的二进制文件,或者更具体地说,如何创建我的流?
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 的 文档 示例无助地使用 File!
The documentation examples for BinaryReader unhelpfully use File!
推荐答案
您始终使用 StorageFile 类:
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屋!
查看全文