Wednesday, October 3, 2018

Simple way to read and write files asynchronously in C#

Two simple ways to read and write files in C#.

async Task<string> ReadFromFileAsync(string filePath)
{
    using (TextReader file = File.OpenText(filePath))
    {
        return await file.ReadLineAsync();
    }
}

async Task WriteToFileAsync(string filePath, string text)
{
    using (TextWriter tw = File.CreateText(filePath))
    {
        await tw.WriteAsync(text);
    }
}

No comments:

Post a Comment