.net 框架是否有一个 async
内置库/程序集,允许使用文件系统(例如 File.ReadAllBytes
、File. WriteAllBytes
)?
或者我是否必须使用 StreamReader
和 StreamWriter
的 async
方法编写自己的库?
像这样的东西会很不错:
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/