4

当 System.IO 命名空间不包含 File 类时,如何在 Windows 应用商店应用程序中读取二进制文件,或者更具体地说,如何创建我的 Stream?

BinaryReader的文档示例无济于事地使用 File!

4

1 回答 1

15

您始终使用StorageFile类访问 Windows 应用商店应用程序中的文件:

StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);

然后,您可以使用 WinRT API 获取文件的二进制内容:

IBuffer buffer = await FileIO.ReadBufferAsync(file);
byte[] bytes = buffer.ToArray();

如果你想使用BinaryReader你需要一个流来代替:

StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("a");
Stream stream = (await file.OpenReadAsync()).AsStreamForRead();
BinaryReader reader = new BinaryReader(stream);

在这种情况下,请确保您只对二进制数据使用ReadBytes(),这不考虑编码。

于 2012-12-13T05:51:51.650 回答