public Task <string> ReadStringAsync(IStorableFileInfo fileInfo) { using (var rs = fileInfo.CreateReadStream()) using (var sr = new StreamReader(rs)) { return(sr.ReadToEndAsync()); } }
/// <summary> /// 异步写入字符串。 /// </summary> /// <param name="fileInfo">给定的 <see cref="IStorableFileInfo"/>。</param> /// <param name="content">给定的写入字符串。</param> /// <returns>返回 <see cref="Task"/>。</returns> public Task WriteStringAsync(IStorableFileInfo fileInfo, string content) { using (var rs = fileInfo.CreateReadStream()) using (var sw = new StreamWriter(rs)) { return(sw.WriteAsync(content)); } }
public async Task ReadAsync(IStorableFileInfo fileInfo, Stream writeStream, CancellationToken cancellationToken = default) { if (writeStream.CanSeek) { writeStream.Seek(0, SeekOrigin.Begin); } using (var rs = fileInfo.CreateReadStream()) { var processingSize = 0L; var processingSpeed = 0L; var beginSecond = DateTime.Now.Second; var readLength = 0; var buffer = new byte[Options.WebFile.BufferSize]; while ((readLength = await rs.ReadAsync(buffer, 0, buffer.Length).DisableAwaitContext()) > 0) { await writeStream.WriteAsync(buffer, 0, readLength, cancellationToken).DisableAwaitContext(); processingSize += readLength; processingSpeed += readLength; if (ProgressAction is not null) { var endSecond = DateTime.Now.Second; if (beginSecond != endSecond) { processingSpeed = processingSpeed / (endSecond - beginSecond); } ProgressAction(new StorageProgressDescriptor { ContentLength = fileInfo.Length, StartPosition = 0, ProcessingSize = processingSize, ProcessingSpeed = processingSpeed, ProcessingPercent = Math.Max((int)(processingSize * 100 / fileInfo.Length), 1) }); if (beginSecond != endSecond) { beginSecond = DateTime.Now.Second; processingSpeed = 0; } } } } }