' The example creates a file named 'WriteFile. String lines = įile.AppendAllLines(Path.Combine(docPath, Convert.ToString('WriteFile.txt')), lines) Create a string array with the lines of text Because the StreamWriter object is declared and instantiated in a using statement, the Dispose method is invoked, which automatically flushes and closes the stream. The following example shows how to use the StreamWriter class to synchronously write text to a new file one line at a time. Example: Synchronously write text with StreamWriter NET via the StreamWriter and StreamReader classes for writing, reading, and appending. A real-world app usually provides more robust error checking and exception handling. In this tutorial, well work with text files (.txt extension) in C. The following examples show only the minimum amount of code needed.