Home >Backend Development >C++ >How Can I Easily Append Text to a File in C#?
Effortlessly Add Text to an Existing File in C#
Need to add a line of text to a file in C# without the fuss? The File.AppendAllText
method is your solution. Append text to your files quickly and easily with just a few lines of code.
The File.AppendAllText
method efficiently adds text to an existing file without erasing the original content. Here's how:
<code class="language-csharp">File.AppendAllText(@"c:\path\file.txt", "text content" + Environment.NewLine);</code>
This code snippet uses File.AppendAllText
to add the string "text content" to the file located at "c:pathfile.txt". Environment.NewLine
ensures the new text starts on a new line.
Running this code seamlessly adds your text, making updating and extending files straightforward. For quick text file updates, remember the simplicity and power of File.AppendAllText
.
The above is the detailed content of How Can I Easily Append Text to a File in C#?. For more information, please follow other related articles on the PHP Chinese website!