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
laai lalalaaPosted Sep 11, 2009, 11:48 AM
Lakhan PalPosted Sep 11, 2009, 7:40 AM
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
Roei BarPosted Sep 11, 2009, 7:39 AM