-1

I have two textbox when I press a button textbox text save in a file, but when I again enter new text it is replace the previously saved text. My code:

using (StreamWriter strw = new StreamWriter("E:\\win part\\Discrete Mathematics\\userrequest.txt"))
{               
  strw.Write(nametextBox.Text+"\t");
  strw.WriteLine(passwordtextBox.Text);
  strw.Close();
  MessageBox.Show("Yor request has been submitted successfully.", "Sucess", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

I want to save this new text after previously saved text. please help me to solve this.

boop_the_snoot
  • 3,209
  • 4
  • 33
  • 44
JAWAD KHAN
  • 41
  • 8
  • did you google ? https://stackoverflow.com/questions/7306214/append-lines-to-a-file-using-a-streamwriter – Rakibul Haq Nov 06 '17 at 03:55
  • You could take a look at [StreamWriter (String, Boolean)](https://msdn.microsoft.com/en-us/library/36b035cb(v=vs.110).aspx). Setting the bool to true tells it to append to the file rather than overwrite. In your case it will be `using (StreamWriter strw = new StreamWriter("E:\\win part\\Discrete Mathematics\\userrequest.txt", true))` – Keyur PATEL Nov 06 '17 at 03:57
  • 1
    Search for "append" – Adam B Nov 06 '17 at 03:57
  • Would `File.AppendAllText("E:\\win part\\Discrete Mathematics\\userrequest.txt", nametextBox.Text + "\t" + passwordtextBox.Text);` work for you? – Enigmativity Nov 06 '17 at 04:01

1 Answers1

0

Adam B is correct, set 'append' parameter to true for the streamWrite constructor:

using (StreamWriter strw = new StreamWriter("E:\\win part\\Discrete Mathematics\\userrequest.txt",true))

...

Joseph Wu
  • 4,786
  • 1
  • 21
  • 19