public async Task WriteAsync(IStorageFileInfo fileInfo, Stream readStream, CancellationToken cancellationToken = default) { fileInfo.NotNull(nameof(fileInfo)); readStream.NotNull(nameof(readStream)); var buffer = new byte[Options.BufferSize]; if (readStream.CanSeek) { readStream.Seek(0, SeekOrigin.Begin); } using (var writeStream = fileInfo.CreateWriteStream()) { var currentCount = 1; while (currentCount > 0) { // 每次从文件流中读取指定缓冲区的字节数,当读完后退出循环 currentCount = await readStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(); // 将读取到的缓冲区字节数写入请求流 await writeStream.WriteAsync(buffer, 0, currentCount, cancellationToken).ConfigureAwait(); if (ProgressAction.IsNotNull()) { var descriptor = new StorageProgressDescriptor( readStream.Length, readStream.Position, writeStream.Length, writeStream.Position, currentCount); ProgressAction.Invoke(descriptor); } } } }
public Task <string> ReadStringAsync(IStorageFileInfo fileInfo) { fileInfo.NotNull(nameof(fileInfo)); using (var readStream = fileInfo.CreateReadStream()) using (var sr = new StreamReader(readStream)) { return(sr.ReadToEndAsync()); } }
public Task WriteStringAsync(IStorageFileInfo fileInfo, string content) { fileInfo.NotNull(nameof(fileInfo)); content.NotEmpty(nameof(content)); using (var readStream = fileInfo.CreateReadStream()) using (var sw = new StreamWriter(readStream)) { return(sw.WriteAsync(content)); } }