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

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

    0 讨论(0)
  • 2020-12-29 04:24

    If you have to write a file in a background task you can write it sincronously in an async method.

    static void Main(string[] args)
    {
        Console.WriteLine("1");
        // thread 1, sync
        byte[] dataToWrite = System.Text.ASCIIEncoding.ASCII.GetBytes("my custom data");
        Task.Run(() =>
        {
            Console.WriteLine("2");
            // aspetto un secondo
            System.Threading.Thread.Sleep(1000);
            // thread 2, async code
            System.IO.File.WriteAllBytes(@"c:\temp\prova.txt", dataToWrite);
            Console.WriteLine("2, end");
        });
    
        Console.WriteLine("3, exit");
    
        Console.ReadLine();
        // thread 1
    }
    

    the execution output will be: 1,3,2,2 end

    0 讨论(0)
  • 2020-12-29 04:30

    In .NET core (since version 2.0) there are now all the async flavors of corresponding ReadAll/WriteAll/AppendAll methods like:

    File.(Read|Write|Append)All(Text|Lines|Bytes)Async
    

    https://docs.microsoft.com/en-us/dotnet/api/system.io.file.readallbytesasync?view=netcore-2.1

    Unfortunately, they are still missing from .NET standard 2.0.

    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 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);
        };
    }
    
    0 讨论(0)
提交回复
热议问题