Home >Backend Development >C++ >How to Append a Single Line to a File in C#?
Append a single line to an existing file
To append a single line to an existing file, use the File.AppendAllText
method. This method allows you to open a file in append mode, add the required lines of text, and then automatically close the file.
How to use File.AppendAllText:
The following code block demonstrates how to use File.AppendAllText
to append a line to a file:
<code class="language-csharp">using System.IO; ... File.AppendAllText(@"c:\path\file.txt", "文本内容" + Environment.NewLine);</code>
In this example, we use File.AppendAllText
to open the file located at 'c:pathfile.txt' in append mode. We append a line of "text content" followed by a newline character, ensuring it is added to the file as a new line. After the code executes successfully, the appended lines are automatically written to the file.
The above is the detailed content of How to Append a Single Line to a File in C#?. For more information, please follow other related articles on the PHP Chinese website!