c# - .net 框架是否提供使用文件系统的异步方法?

标签 c# asynchronous filesystems async-await system.io.file

.net 框架是否有一个 async 内置库/程序集,允许使用文件系统(例如 File.ReadAllBytesFile. WriteAllBytes)?

或者我是否必须使用 StreamReaderStreamWriterasync 方法编写自己的库?

像这样的东西会很不错:

var bytes = await File.ReadAllBytes("my-file.whatever");

最佳答案

Does the .net framework has an async built-in library/assembly which allows to work with the file system

是的。有用于处理文件系统的异步方法,但没有用于静态 File 类型的辅助方法。它们在 FileStream 上。

因此,没有 File.ReadAllBytesAsync 但有 FileStream.ReadAsync 等。例如:

byte[] result;
using (FileStream stream = File.Open(@"C:\file.txt", FileMode.Open))
{
    result = new byte[stream.Length];
    await stream.ReadAsync(result, 0, (int)stream.Length);
}

关于c# - .net 框架是否提供使用文件系统的异步方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35750489/

相关文章:

c# - Gridview RowDataBound 显示每一行的隐藏列

c# - 使用 LINQ 对字符串数组进行排序

c# - 将大图像文件上传到 SharePoint 时出现问题

c# - 在等待时,当控制权转到调用者时,它是否处理 for 循环的下一次迭代(等待返回任务吗?)?

javascript - 关于在 lambda 上保存图像

.net - Nant fileset basedir 模式

c# - String.Split()方法: compiler chooses wrong overload

javascript - 来自第 3 方 API 的回调未在 MeteorJS 中获取当前用户

javascript - Async-WaterFall 没有按预期工作

python - Django不能上传文件和图片