How to add new line into txt file in C#
Appending text to an existing file in C# is very similar to writing to a new file, but you need to use a different constructor for the StreamWriter class that allows you to specify that you want to append to the file instead of overwriting it. Here are the steps to append text to an existing file in C#:
- Create a StreamWriter object that appends to the existing file
- Write to the file
- Close the StreamWriter object
Create a StreamWriter object that appends to the existing file
To append text to an existing file, you will first need to create a StreamWriter object that appends to the file. You will need to specify the file path and name, and set the second parameter to true to indicate that you want to append to the file instead of overwriting it.
Following is an example of how to create a StreamWriter object that appends to an existing file:
Write to the file
Once you have created the StreamWriter object, you can use its methods to append to the file. You can use the same Write and WriteLine methods as before to write text to the file.
Following is an example of how to append text to a file using the StreamWriter object:
Close the StreamWriter object
After you have finished appending text to the file, you should close the StreamWriter object to release any resources it is using. You can do this by calling the Close method on the object.
Following is an example of how to close the StreamWriter object:
Append text to an existing file | C#
Putting it all together, following is an example of how to append text to an existing file in C#:
This will append the text "This text will be appended to the file. " and "This is a new line that will be appended." to the existing "file.txt" file in the same directory as the program.
- C# Crystal Reports Tutorial
- CSharp Communication Tutorial
- Threading in C#
- C# Ado.Net Tutorial and Source Code , c# database programming tutorial
- C# ADO.NET data Providers Tutorial
- C# Dataset Tutorial
- C# DataAdapater Tutorial
- Csharp DataView Tutorial
- Csharp Remoting Tutorial
- C# XML Tutorial
- C# DataGridView Tutorial
- C# Advanced Topics