Insert data in a file
Posted on 2003-02-25
Hi. I want to insert some data in a file. The file allready exists and has data. So I want to write in this file but not at the end of file, whithout overwritting the existing data in this possition and without using a temporary file. I tried this code but doesn't works because the new data overwrites the old:
Dim fs As FileStream = New FileStream("data.bin", FileMode.OpenOrCreate, FileAccess.Write)
Dim w As BinaryWriter = New BinaryWriter(fs) ' create a BinaryWriter
w.BaseStream.Seek(4, SeekOrigin.Begin) ' seek the beginning of the file after 4bytes
The code above overwrites the data from 4th byte to 8th byte (because Int32 is 4 bytes) and writes there the new data.