using System.IO; string filePath = @"C:\Example\text.txt"; using (StreamWriter writer = new StreamWriter(filePath)) { writer.WriteLine("Hello, world!"); }
using System.IO; string filePath = @"C:\Example\text.txt"; using (StreamWriter writer = new StreamWriter(filePath)) { writer.WriteLine("Line 1"); writer.WriteLine("Line 2"); writer.WriteLine("Line 3"); }This example is similar to the first one, but instead of writing a single line to the file, we write three lines using three consecutive calls to the `WriteLine` method. Overall, StreamWriter is a useful class for writing textual data to a file in C#. It is part of the System.IO namespace and can be found in the .NET Class Library and the .NET Core Class Library.