I want to read a locally stored file into a byte array. How do I do that? This is my try:
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(filePath);
var file = await folder.GetFileAsync(filePath);
var buffer = await FileIO.ReadBufferAsync(file);
DataReader dataReader = Windows.Storage.Streams.DataReader.FromBuffer(buffer);
// doesn't work because ReadBytes wants a byte[] as parameter and also isn't asynchronous
byte[] result = dataReader.ReadBytes(buffer.Length);
I think the other answers make things unnecessarily complicated. There is a convenient extension method IBuffer.ToArray()
for this purpose.
Simply do this:
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Storage;
...
IStorageFile file;
IBuffer buffer = await FileIO.ReadBufferAsync(file);
byte[] bytes = buffer.ToArray();
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await storageFolder.GetFileAsync(FILE_NAME);
byte[] result;
using (Stream stream = await sampleFile.OpenStreamForReadAsync())
{
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
result = memoryStream.ToArray();
}
}
Three concepts come to my mind - using FileStream and modyfing your method little:
the first reads bytes via a provided buffer, though this method needs seekable stream:
FileOpenPicker picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".txt");
using (Stream fileStr = await (await picker.PickSingleFileAsync()).OpenStreamForReadAsync())
{
byte[] bytes = new byte[fileStr.Length];
const int BUFFER_SIZE = 1024;
byte[] buffer = new byte[BUFFER_SIZE];
int position = 0;
int bytesread = 0;
while ((bytesread = await fileStr.ReadAsync(buffer, 0, BUFFER_SIZE)) > 0)
for (int i = 0; i < bytesread; i++, position++)
bytes[position] = buffer[i];
}
the second method asynchronously copies filestream to memory then gets it as an array:
using (MemoryStream memStream = new MemoryStream())
using (Stream fileStr = await (await picker.PickSingleFileAsync()).OpenStreamForReadAsync())
{
await fileStr.CopyToAsync(memStream);
byte[] bytes = memStream.ToArray();
}
your method with little mdification - processing via memory stream:
var buffer = await FileIO.ReadBufferAsync(file);
using (MemoryStream mstream = new MemoryStream())
{
await buffer.AsStream().CopyToAsync(mstream);
byte[] result = mstream.ToArray();
}
Or maybe better you can avoid using byte[] and instead use IBuffer or MemoryStream.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With