Append text to end of line in file
hello:),
i'm using c# and i want to read a file line by line, and, if some condition occurs, append a string at the end of the read line. how can this be done? with StreamReader the cursor gets to the next line.
thanks

Roei BarRank #1,271 · 1.4K reputationPosted Sep 11, 2009, 3:39 AM EDT
using System; using System.IO; class Test { public static void Main() { string path = @"c:\temp\MyTest.txt"; // Open the file to read from. string[] readText = File.ReadAllLines(path); int _length = readText.Length; for (i=0;i) { readText[i] += @"\n" + ; } } // This text is added only once to the file. if (!File.Exists(path)) { File.Delete(path); // Create a file to write to. File.WriteAllLines(path, readText); } } }Lakhan Pal37 reputationPosted Sep 11, 2009, 3:40 AM EDT
Use StringBuilder for This....
(1) Read the File Contents line by Line
(2) Add that line to the StringBuilder's object.
(3) When any condition is met then add your required data into the StringBuilder's object for that as per your requirement.
(4) This way you will finally get all the contents in a string builder now overwrite the existing file..
Use this code to Overwrite Data:
string HTMLFilePath ={PhySical Path of your File with Name}
StreamWriter sw = new StreamWriter(HTMLFilePath);
sw.Write(HTMLContents.ToString());
sw.Close();
in the above Code HTMLContents are the contents of stringBuilder
laai lalalaa3 reputationPosted Sep 11, 2009, 7:48 AM EDT