我发现 StorageFile WriteAsync 操作有一个奇怪的行为。当我创建大文件(大约 4gb)然后尝试在此文件末尾写入时,异步写入操作需要很长时间。看起来它填补了开始和写入位置之间的空白。无论如何,进程管理器显示磁盘使用率很高。是 WinRT 问题还是我做错了什么?如何解决?
const long FILE_SIZE = 4294967296;
var folderPicker = new FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
folderPicker.FileTypeFilter.Add("*");
var folder = await folderPicker.PickSingleFolderAsync();
var file = await folder.CreateFileAsync("1.iso", CreationCollisionOption.ReplaceExisting);
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
stream.Size = FILE_SIZE; // size 4 Gb
var buffer = new byte[1]; // create one byte buffer
buffer[0] = 0xFF;
stream.Seek(FILE_SIZE - 10); // seek almost at the and position
await stream.WriteAsync(buffer.AsBuffer()).AsTask(); // write operation takes a lot of time
await stream.FlushAsync();
}