.net框架是否提供用于处理文件系统的异步方法? [英] Does the .net framework provides async methods for working with the file-system?

查看:111
本文介绍了.net框架是否提供用于处理文件系统的异步方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.net框架是否具有允许使用文件系统(例如File.ReadAllBytesFile.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)?

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆