0

I'm new to C# Programming and I need to write data to csv file but with this code it writes into file but also deletes previous info in file.What should i do for write on top of existing info in csv file ?

var records = new List<user>
{
    new user { Username = tBnewusername.Text, Password = Functions.ComputeSha256Hash(tbnewpassword.Text) },
};

using (var writer = new StreamWriter(Functions.bingPathToAppDir("\\data\\users.csv")))
{
    using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
    {
        csv.WriteRecords(records);

    }

    MessageBox.Show("New User Was Created");
}
Tlamir
  • 23
  • 4
  • 1
    Did you check the **[online documentation](https://learn.microsoft.com/en-us/dotnet/api/system.io.streamwriter.-ctor?view=netframework-4.8#System_IO_StreamWriter__ctor_System_String_System_Boolean_)** to see if there was a constructor overload which would allow you to append to the file? – Ňɏssa Pøngjǣrdenlarp Mar 05 '20 at 20:21
  • 1
    See [Manipulate existing CSV file, while keeping columns order. (CsvReader/CsvWriter)](https://stackoverflow.com/q/58984212) and [Can write to csv file but not append](https://stackoverflow.com/q/40688047), both of which have answers suggesting use of `new StreamWriter(path, true)` to append to a file. – dbc Mar 05 '20 at 20:29

0 Answers0