Reading files asynchronously
In this section, we will read text from a file asynchronously. We will be building upon the code from the previous section that writes the text to a file asynchronously.
The following steps will add our asynchronous read method and update the Program.cs
file to run our asynchronous code:
- In the
FileReadWriteAsync
class, add the following method:public static async Task<string> ReadTextAsync() { Â Â Â Â string filePath = @"C:\Temp\Greetings.txt"; Â Â Â Â using (FileStream fileStream = new FileStream( Â Â Â Â Â Â Â Â Â Â Â Â filePath, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â FileMode.Open, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â FileAccess.Read, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â FileShare.Read...