using System.IO; StreamWriter writer = new StreamWriter("path/to/file.txt"); writer.Write("Hello, World!"); writer.Close();
using System.IO; StreamWriter writer = new StreamWriter("path/to/file.txt"); writer.WriteLine("Hello, World!"); writer.WriteLine("How are you today?"); writer.Close();
using System.IO; using (StreamWriter writer = new StreamWriter("path/to/file.txt")) { writer.WriteLine("Hello, World!"); }In this example, we use the StreamWriter object inside a using statement. This allows us to automatically dispose of the object when it is no longer needed, without the need for an explicit call to the Close() method. The StreamWriter class is part of the System.IO namespace in the .NET Framework.