我想有一個方法,它將一個System.IO.Stream作爲輸入並使用它將數據寫入文件。到目前爲止,我有以下幾點:如何將數據從流保存到WinRT中的文件?
public async Task SaveStreamToFileX(Stream stream, string filePath, IProgress<long> progress)
{
var folder = await StorageFolder.GetFolderFromPathAsync(Path.GetDirectoryName(filePath));
var fileName = Path.GetFileName(filePath);
StorageFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);
var istream = stream.AsInputStream();
var canRead = stream.CanRead; //this returns true
using (var reader = new DataReader(istream))
{
using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
using (IOutputStream outputStream = fileStream.GetOutputStreamAt(0))
{
using (DataWriter writer = new DataWriter(outputStream))
{
IBuffer buffer;
long readBytes = 0;
const int bufferSize = 8192;
while ((buffer = reader.ReadBuffer(bufferSize)).Length > 0) //exception System.Exception with message: Out of range ...
{
writer.WriteBuffer(buffer);
readBytes += bufferSize;
progress.Report(readBytes);
}
}
}
}
}
}
問題是,當我嘗試讀while循環(先讀)數據的異常(超出範圍)被拋出。流應該有數據。我不確定是否這麼長的代碼是壞的,如果有人有更好的解決方案,它會很好。 附註: 如果我嘗試await reader.LoadAsync(50)
它返回50.我不確定LoadAsync應該做什麼。也許我必須在閱讀之前調用它以準備讀取數據?我將進一步調查... 此外,Stream.CanRead返回true。