Take a look at FileStream.WriteAsync (note that you should use the correct overload, which takes a bool indicating whether it should start async:)
public async Task WriteAsync(string data) { var buffer = Encoding.ASCII.GetBytes(data); using (var fs = new FileStream(@"File", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, buffer.Length, true)) { await fs.WriteAsync(buffer, 0, buffer.Length); } }
Edit
If you want to use string data and avoid conversion to byte[] , you can use the more abstract and less detailed StreamWriter.WriteAsync overload, which takes a string:
public async Task WriteAsync(string data) { using (var sw = new StreamWriter(@"FileLocation")) { await sw.WriteAsync(data); } }
source share