I have the following code written as I want to save a string to the end of a text file in the form of a list.
However, at the moment when I try to save something, it deletes all previous text in the file and just adds the most recent one. The following is my code:
using (var writer = new StreamWriter(@"C:\Users\Falconex\Documents\Visual Studio 2015\Projects\Test2\Test2\bin\Debug\Product.txt"))
{
writer.WriteLine(productTextBox.Text + Environment.NewLine);
writer.Close();
}
Thank you in advance, Lucy
You're currently just overwriting the contents of the file, every time you write.
StreamWriter has a second parameter you have to set to true to append text: https://msdn.microsoft.com/library/36b035cb(v=vs.110).aspx
using (var writer = new StreamWriter(@"C:\Path\To\file.txt", true))
{
writer.WriteLine(productTextBox.Text + Environment.NewLine);
writer.Close();
}