using System.IO; // Create a StreamWriter object TextWriter writer = new StreamWriter("test.txt"); // Write text to the file writer.WriteLine("Hello World!"); writer.WriteLine("This is a test file."); // Close the writer writer.Close();
using System; using System.IO; // Create a StreamWriter object for the console TextWriter writer = Console.Out; // Write text to the console writer.WriteLine("Hello World!"); // Flush the writer to ensure text is written to the console writer.Flush();Both of these examples use the System.IO namespace, which is part of the .NET Framework Class Library.