matthew king

matthew king

  • NA
  • 52
  • 0

help with stream builder

Sep 7 2009 9:55 AM
I have a form with three textboxes and a button. The user enters data into the said textboxes and pushes a button. Upon clicking, streamreader queries a txt file for specific lines. I'm trying to overwrite these three lines with the data from my textboxes. However, instead of overrating these lines, it just places new lines below the original lines. So, I end up with six lines instead of (three with the original data and three with data from texboxes 1 - 3). How can I get this to overwrite the orginal three lines with the new data? private void button23_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); string line = string.Empty; // path of the file using (StreamReader fs = new StreamReader(@"c:\test.txt", true)) { //checks until end of file while ((line = fs.ReadLine()) != null) { // This will serch a txt file for the word payload if (line.ToLower().StartsWith("center")) //This will override the sentence.... sb.AppendLine("Center1 = This is a test" + textbox1 + "end of test"); if (line.ToLower().StartsWith("left")) //This will override the sentence.... sb.AppendLine("Left = This is a test" + textbox2 + "end of test"); if (line.ToLower().StartsWith("right")) //This will override the sentence.... sb.AppendLine("Right = This is a test" + textbox3 + "end of test"); sb.AppendLine(line); } } // path to my file, // don't forget to close streamwriter // This should overwrite the lines. using (StreamWriter sw = new StreamWriter(@"c:\test", false)) { sw.Write(sb.ToString()); sw.Close(); } }

Answers (4)