.net框架是否提供用于处理文件系统的异步方法? [英] Does the .net framework provides async methods for working with the file-system?
问题描述
.net框架是否具有允许使用文件系统(例如File.ReadAllBytes
,File.WriteAllBytes
)的async
内置库/程序集?
Does the .net framework has an async
built-in library/assembly which allows to work with the file system (e.g. File.ReadAllBytes
, File.WriteAllBytes
)?
还是我必须使用StreamReader
和StreamWriter
的async
方法编写自己的库?
Or do I have to write my own library using the async
Methods of StreamReader
and StreamWriter
?
类似这样的东西会很不错:
Something like this would be pretty nice:
var bytes = await File.ReadAllBytes("my-file.whatever");
推荐答案
.net框架是否具有异步内置库/程序集,该库/程序集允许使用文件系统
Does the .net framework has an async built-in library/assembly which allows to work with the file system
是的.有用于处理文件系统的异步方法,但没有用于静态File
类型的帮助器方法的异步方法.它们在FileStream
上.
Yes. There are async methods for working with the file system but not for the helper methods on the static File
type. They are on FileStream
.
因此,没有File.ReadAllBytesAsync
,但是有FileStream.ReadAsync
,依此类推.例如:
So, there's no File.ReadAllBytesAsync
but there's FileStream.ReadAsync
, etc. For example:
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);
}
这篇关于.net框架是否提供用于处理文件系统的异步方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!