' The example creates a file named 'WriteFile. NET via the StreamWriter and StreamReader classes for writing, reading, and appending. String lines = įile.AppendAllLines(Path.Combine(docPath, Convert.ToString('WriteFile.txt')), lines) In this tutorial, well work with text files (.txt extension) in C. 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 A real-world app usually provides more robust error checking and exception handling.
The following examples show only the minimum amount of code needed.