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

前端 未结 5 1686
离开以前
离开以前 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:18

    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)?

    Unfortunately, the desktop APIs are a bit spotty when it comes to asynchronous file operations. As you noted, a number of the nice convenience methods do not have asynchronous equivalents. Also missing are asynchronous opening of files (which is especially useful when opening files over a network share).

    I'm hoping these APIs will be added as the world moves to .NET Core.

    Or do I have to write my own library using the async Methods of StreamReader and StreamWriter?

    That's the best approach right now.

    Note that when using ReadAsync/WriteAsync and friends, you must explicitly open the file for asynchronous access. The only way to do this (currently) is to use a FileStream constructor overload that takes a bool isAsync parameter (passing true) or a FileOptions parameter (passing FileOptions.Asynchronous). So you can't use the convenience open methods like File.Open.

提交回复
热议问题