using System.IO; using System.Text; class Program { static void Main(string[] args) { string path = "example.txt"; string text = "This is a sample text."; // Create a file and write text to it using StreamWriter using (StreamWriter writer = new StreamWriter(path)) { writer.Write(text); } } }
using System.IO; using System.Text; class Program { static void Main(string[] args) { string path = "example.txt"; string text = "This is some additional text."; // Append text to an existing file using StreamWriter using (StreamWriter writer = new StreamWriter(path, true)) { writer.Write(text); } } }This example shows how to append to an existing file instead of overwriting it. We specify the "append" parameter as "true" in the StreamWriter constructor to achieve this. The package library for System.IO is part of the .NET Framework, which is a software framework developed by Microsoft.