using System.IO; class Program { static void Main() { // Create a new text file called "output.txt" using (TextWriter writer = new StreamWriter("output.txt")) { // Write some text to the file writer.WriteLine("Hello, world!"); } } }
using System.IO; class Program { static void Main() { // Append text to an existing file called "output.txt" using (TextWriter writer = new StreamWriter("output.txt", true)) { // Write some text to the file writer.WriteLine("This is some additional text!"); } } }In this example, we opened an existing file called "output.txt" with the StreamWriter constructor that takes two arguments: the file name and a boolean value indicating whether to append to the file or overwrite it. We passed "true" to append to the file. We then used the WriteLine method to add the text "This is some additional text!" to the end of the file. The TextWriter class is part of the System.IO namespace in the .NET Framework class library.