Does the .net framework provides async methods for working with the file-system?

前端 未结 5 1693
离开以前
离开以前 2020-12-29 03:48

Does the .net framework has an async built-in library/assembly which allows to work with the file system (e.g. File.ReadAllBytes, File.WriteA

5条回答
  •  一个人的身影
    2020-12-29 04:34

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

    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.

    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);
    }
    

提交回复
热议问题