user2140611
user2140611

Reputation:

Add value from textbox to csv file without replacing data

private void button1_Click(object sender, EventArgs e) {
    String[] bla = File.ReadAllText(@"c:\\chat.csv").Split('|');

    for (int i = 0; i < bla.Length; i++) {
        textBox1.AppendText(bla[i] + Environment.NewLine); 
    }
}

This code loads a csv file into textBox1 and splits lines (|). I would like to add text to the csv file with the input of textBox2. So, the value of textBox2 should be added to the last line in chat.csv.

 waarde1 = textBox2.Text;
 File.WriteAllText(@"c:\\chat.csv", waarde1); */

I tried to create (I am a beginner) something. The above code writes the text in textBox2 to the csv file but it also deletes all the current.

What function can I use so that the data won't be removed?

Upvotes: 0

Views: 3037

Answers (2)

dqm
dqm

Reputation: 1350

Do you mean to keep the text and add some new text?

File.AppendAllText("C:\\chat.csv", "newline");

Upvotes: 0

bash.d
bash.d

Reputation: 13207

Instead of File.WriteAllText use

waarde1 = textBox2.Text;
File.AppendAllText(@"c:\\chat.csv", waarde1);

This will open a new stream and append the text.

See MSDN for more on this.

Upvotes: 1

Related Questions