How do I read a binary file in a Windows Store app?

前端 未结 1 1056
一个人的身影
一个人的身影 2021-01-19 04:52

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?

The docume

相关标签:
1条回答
  • 2021-01-19 05:00

    You always access files in Windows Store apps using StorageFile class:

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

    You can then get the binary contents of the file using the WinRT APIs:

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

    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);
    

    Make sure you only use ReadBytes() for binary data in this case which doesn't take encoding into account.

    0 讨论(0)
提交回复
热议问题