using System.IO; // Create a new file and write some text to it using StreamWriter string filePath = "example.txt"; using (StreamWriter writer = new StreamWriter(filePath)) { writer.WriteLine("This is some example text."); }
using System.IO; // Append some text to an existing file using StreamWriter string filePath = "example.txt"; using (StreamWriter writer = new StreamWriter(filePath, true)) { writer.WriteLine("This text will be appended to the end of the file."); }In this example, we append some text to an existing file by passing `true` as the second argument to the StreamWriter constructor. This tells the StreamWriter to open the file in append mode, so any text written to it will be added to the end of the file instead of overwriting its contents. Package/library: System.IO.