string filePath = "D:\\DemoProject\\DemoFolder\\codemantra99.txt";
if (File.Exists(filePath))
{
File.AppendAllText(filePath, txtUserText.Text + Environment.NewLine);
lblMessage.Text = "Text appended successfully.";
}
else
{
lblMessage.Text = "Error!!!! Folder path does not exists.";
}
In above code i have taken the path of the file in a string type variable. After defining the path i have validated the file exists or not. If file does not exist will display a message to user. File.AppendAllText(path, text) to append the user entered text in existing text file. to append the text in a new line Environment.NewLine have been added. Now we have done. lets run the code and check the output.Append Text to Existing Text File C#.Net
How to append text in existing text file in a path using C#.Net. File.AppendAllText(path, text) to append text. Add Environment.NewLine for new line.
In today's article I will show you a simple tutorial with an example how you can append the text to an existing file in c#.net. In this example I will use windows application with c#.net. Now for example fist we will create a folder and add a text file at that location with sone text. In above location I have added a text file. In this file I have added some text. So, we will append the user text with the existing text. Let's check the content of the text.
After this we will create a new windows application with C#.Net. In this application we will add a form, and, in this form, we will add a textbox and a button control.
Now let's add the click event of button control and add the below code in it.
Now let's check the text file for final output.