Help with stream builder
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();
}
}

Roei BarRank #1,271 · 1.4K reputationPosted Sep 7, 2009, 7:23 AM EDT
List textList = new List(); List newTextList = new List(); textList.AddRange(File.ReadAllLines(filepath); foreach (string line in textList) { string newLine = string.Empty; if (line.ToLower().StartsWith("center")) { newLine = "Cneter = This is a test" + textbox2 + "end of test"; } if (line.ToLower().StartsWith("left")) { newLine = "Left = This is a test" + textbox2 + "end of test"; } if (line.ToLower().StartsWith("right")) { newLine = "Right = This is a test" + textbox2 + "end of test"; } if(newLine != line) newTextList.Add(newline); else newTextList.Add(line); } // Write to file File.WriteAllLines(filepath, textList.ToArray());matthew king51 reputationPosted Sep 7, 2009, 8:36 AM EDT
Roei BarRank #1,271 · 1.4K reputationPosted Sep 7, 2009, 12:51 PM EDT
i got mixed up in code :
List textList = new List(); List newTextList = new List(); //set filepath as the path to your test file the entire path //like @"C:\\test.txt" //It will read all lines and then run on the list //Performing the logic you specified for lines that are from the type //you looked for, insert the rows in the same order to a new List //and the //File.WriteAllLines(filepath,newTextList.ToArray()); //will overwrite the file with the new linestextList.AddRange(File.ReadAllLines(filepath);
foreach (string line in textList)
{
string newLine = string.Empty;
if (line.ToLower().StartsWith("center"))
{
newLine = "Cneter = This is a test" + textbox2 + "end of test";
}
if (line.ToLower().StartsWith("left"))
{
newLine = "Left = This is a test" + textbox2 + "end of test";
}
if (line.ToLower().StartsWith("right"))
{
newLine = "Right = This is a test" + textbox2 + "end of test";
}
if(newLine != line)
newTextList.Add(newline);
else
newTextList.Add(line);
}
// Write to file
File.WriteAllLines(filepath,
newTextList.ToArray());matthew king51 reputationPosted Sep 7, 2009, 5:21 PM EDT