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

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

    It already does it. See for example Using Async for File Access MSDN article.

    private async Task WriteTextAsync(string filePath, string text)
    {
        byte[] encodedText = Encoding.Unicode.GetBytes(text);
    
        using (FileStream sourceStream = new FileStream(filePath,
            FileMode.Append, FileAccess.Write, FileShare.None,
            bufferSize: 4096, useAsync: true))
        {
            await sourceStream.WriteAsync(encodedText, 0, encodedText.Length);
        };
    }
    

提交回复
热议问题