Writing files asynchronously
In this section, we will write text to a file asynchronously. Scenarios where asynchronous file writing can be useful include writing large volumes of text and data to files that will not be read immediately.
Use the following steps to write our code:
- On your
C:\
drive, add a folder calledTemp
if one does not already exist. - Open the
CH16_AsynchronousProgramming
project. - Add a class called
FileReadWriteAsync
. - Add the following method:
public static async Task WriteTextAsync() { string filePath = @"C:\Temp\Greetings.txt"; string text = "Hello, World!"; byte[] encodedText = Encoding.Unicode.GetBytes(text); using (FileStream fileStream = new FileStream( filePath, &...