Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Write to a textFile without substituting text 1

Status
Not open for further replies.

Programming2007

Programmer
Nov 10, 2006
24
US
Hello I am trying to write to a textfile at a certian position. The problem is that when I do that what I write to the file writes over the existing data. How do I do a write that is like an insert which just inserts the values into the textFile without replacing them. Below is my code:

FileStream fs = new FileStream(cabFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs,Encoding.UTF8);
StreamReader streamReader = new StreamReader(fs);
str_rawFileData = streamReader.ReadToEnd();

RecordHeader = str_rawFileData.Substring(0, startIndex);
fs.Position = RecordHeader.Length;
sw.Write("|");
 
Create a new file, read from the old file up to the insertion point, write this to the new file. Write the text to be inserted to the new file. Read the remaining text from the old file and write this to the new file. Delete the old file and rename the new file with the old file's name.

Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top