0

I have a text box set up for the user to enter their name. When they enter it in it saves it in the notepad but overwrites the previous name. I want the text file to store all the names not just the most recently entered.

using (StreamWriter objWriter = new StreamWriter(@"..\..\..\Files\playerdetails.txt"))
        {
            objWriter.Write(txtName.Text);

            MessageBox.Show("You are now ready to play");
            Form1 myForm1 = new Form1();
            myForm1.Show();
        }

1 Answer 1

2

You can just use File.AppendAllText method:

File.AppendAllText(@"..\..\..\Files\playerdetails.txt", txtName.Text);
Sign up to request clarification or add additional context in comments.

5 Comments

I changed objwriter.write(txtname.text); to file.apendalltext("path", text); but the notepad is blank after i entered a name in the text box
probably you write to file before there is any text on in the field. If you have just changed code shown in your question, you write to file even before you show the form. So there will be nothing in the text field
With the code i posted above it saves the most recent name in the text file but i want every name to be saved in the text file not just 1
@john get rid of all StreamReader and other stuff. And replace the path with actual path and text with txtName.Text
sorry @selman22 i don't really understand what you mean. If it isn't too much bother could you write out exactly what i should do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.