Working with files asynchronously
This recipe walks us through how to create a file and how to read and write data asynchronously.
Getting ready
To step through this recipe, you will need Visual Studio 2015. There are no other prerequisites. The source code for this recipe can be found at BookSamples\Chapter9\Recipe1
.
How to do it...
To understand how to work with files asynchronously, perform the following steps:
Start Visual Studio 2015. Create a new C# console application project.
In the
Program.cs
file, add the followingusing
directives:using System; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Console; using static System.Text.Encoding;
Add the following code snippet below the
Main
method:const int BUFFER_SIZE = 4096; static async Task ProcessAsynchronousIO() { using (var stream = new FileStream( "test1.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None, BUFFER_SIZE)) { WriteLine($"1. Uses I/O Threads: {stream...