1 Replies - 771 Views - Last Post: 16 October 2011 - 09:46 AM Rate Topic: -----

#1 Pawn1966   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-June 11

Write to a .txt file help

Posted 16 October 2011 - 09:29 AM

I am attempting to write two strings to a .txt file at the same time. I need to be able to keep doing this. My problem is I got it to write once but the code always forced the new information to write over the old information i put in. So i need to be able to keep writing to the .txt file because i will need to recall it to a listbox.

currently attempting this code but i get errors
        private void button1_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text;
            string email = textBox2.Text;
            using (System.IO.StringWriter file = new System.IO.StringWriter(@"F:\Docs\school\contact.txt","name " + name "email" + email" /n"))

        }


I am going to condense my two strings into one string I think to make this easier and just have
string contact = name + " " + email;
I think that might make it easier to post the information i need.

This post has been edited by [email protected]: 16 October 2011 - 09:46 AM
Reason for edit:: fixed messed up code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Write to a .txt file help

#2 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Write to a .txt file help

Posted 16 October 2011 - 09:46 AM

Hi,

Why are you using a StringWriter? If you want to write to a text file, I think you want StreamWriter :):

using (TextWriter writer = new StreamWriter(@"F:\Docs\school\contact.txt", true)) {
       writer.WriteLine(String.Format("{0} {1}", name, email));
}



Notice that I passed true as the second argument to the StreamWriter. This will mean that the text gets appended to the file, rather than overwriting existing content.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1