using System.IO; using System.Threading.Tasks; public static async Task WriteToFileAsync(string fileName, string content) { using (var writer = new StringWriter()) { await writer.WriteAsync(content); await writer.FlushAsync(); // Write content to file await File.WriteAllTextAsync(fileName, writer.ToString()); } }
using System.IO; using System.Threading.Tasks; public static async TaskIn this example, the ReadFromStreamAsync method takes a Stream object as a parameter. It creates a StreamReader and a StringWriter object and uses them to read lines from the stream and write them to the StringWriter asynchronously. It calls the FlushAsync method after writing each line to ensure that the content is written to the StringWriter's buffer. Finally, it returns the content as a string. Package library: System.IOReadFromStreamAsync(Stream stream) { using (var reader = new StreamReader(stream)) using (var writer = new StringWriter()) { while (!reader.EndOfStream) { var line = await reader.ReadLineAsync(); await writer.WriteAsync(line); await writer.FlushAsync(); } return writer.ToString(); } }